From e8897e60e00425e59d705a320bac51dc561443aa Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Wed, 23 Aug 2017 18:52:53 -0400 Subject: Comments and GNOME link --- vala.html.markdown | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 vala.html.markdown (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown new file mode 100644 index 00000000..9a5215bb --- /dev/null +++ b/vala.html.markdown @@ -0,0 +1,25 @@ +--- +language: vala +contributors: + - ["Milo Gilad", "https://github.com/bigbash"] +filename: LearnVala.vala +--- + +In GNOME's own words, "Vala is a programming language that aims to bring modern programming language features to GNOME developers without imposing any additional runtime requirements and without using a different ABI compared to applications and libraries written in C." + +Vala has aspects of Java and C#, so it'll be familiar to those who know either or. + +[Read more here.](https://wiki.gnome.org/Projects/Vala) + +```vala +// Single line comment + +/* Multiline +Comment */ + +/** +* Documentation comment +*/ + + +``` \ No newline at end of file -- cgit v1.2.3 From ca5d90eb7d44439e40c4937dd5b4f55cd2994a74 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Thu, 24 Aug 2017 15:54:48 -0400 Subject: Finished basic datatypes and basic io --- vala.html.markdown | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) mode change 100644 => 100755 vala.html.markdown (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown old mode 100644 new mode 100755 index 9a5215bb..b6cbeb98 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -21,5 +21,31 @@ Comment */ * Documentation comment */ +/* +Data Types +*/ + +// Vala supports the data types supported by most other programming languages. + +char character = 'a' +unichar unicode_character = 'u' // 32-bit unicode character + +int i; // ints can also have guaranteed sizes (e.g. int64, uint64) +uint j; + +long k; + +short l; +ushort m; + + + +/* +Basic Syntax +*/ + +// Like in C#, scope is defined using braces. +// An object or reference is only valid between braces. + -``` \ No newline at end of file +``` -- cgit v1.2.3 From 0c105ca0b23f5e5d83876d216be6a4812425954b Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Thu, 24 Aug 2017 16:10:16 -0400 Subject: Added arrays section --- vala.html.markdown | 65 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 11 deletions(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index b6cbeb98..a8d90e47 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -1,7 +1,7 @@ --- language: vala contributors: - - ["Milo Gilad", "https://github.com/bigbash"] + - ["Milo Gilad", "https://github.com/Myl0g"] filename: LearnVala.vala --- @@ -12,6 +12,7 @@ Vala has aspects of Java and C#, so it'll be familiar to those who know either o [Read more here.](https://wiki.gnome.org/Projects/Vala) ```vala + // Single line comment /* Multiline @@ -21,31 +22,73 @@ Comment */ * Documentation comment */ -/* -Data Types -*/ +/* Data Types */ // Vala supports the data types supported by most other programming languages. char character = 'a' unichar unicode_character = 'u' // 32-bit unicode character -int i; // ints can also have guaranteed sizes (e.g. int64, uint64) -uint j; +int i = 2; // ints can also have guaranteed sizes (e.g. int64, uint64) +uint j = -6; // Won't compile; unsigned ints can only be positive long k; short l; ushort m; +string text = "Hello,"; // Note that the == operator will check string content +string verbatim = """This is a verbatim (a.k.a. raw) string. Special characters +(e.g. \n) are not interpreted. They may also be multiple lines long."""; -/* -Basic Syntax -*/ +// String Templates allow for easy string formatting +string string_template = @"$text world"; // "$text" evaluates to "Hello" + +int test = 5; +int test2 = 10; +string template2 = @"$(test * test2) is a number."; // Expression evaluation + +string template_slice = string_template[7:12]; // => "world" + +// Most data types have methods for parsing. + +bool parse_bool = bool.parse("false"); // => false +int parse_int = int.parse("-52"); +string parse_string = parse_int.to_string(); + +/* Basic I/O */ + +stdout.printf(parse_string); // Prints to console +string input = stdin.read_line(); // Gets input from console + +stderr.printf("Error message"); // Error printing + +/* Arrays */ + +int[] int_array = new int[10]; // Array of ints with 10 slots +int[] int_array2 = {5, 10, 15, 20}; // Can be created on-the-fly + +int[] array_slice = int_array2[1:3]; // Slice (copy of data) +unowned int[] array_slice_ref = int_array2[1:3]; // Reference to data + +// Multi-dimensional Arrays (defined with a number of commas in the brackets) + +int[,] multi_array = new int[6,4]; // 6 is the number of arrays, 4 is their size +int[,] multi_array2 = {{7, 4, 6, 4}, + {3, 2, 4, 6}, + {5, 9, 5, 1}}; +multi_array2[2,3] = 12; // 2 is the array, 3 is the index in the array -// Like in C#, scope is defined using braces. -// An object or reference is only valid between braces. +struct Closet { + public uint shirts; + public uint jackets; +} +enum HouseSize { + SMALL, + MODERATE, + BIG +} ``` -- cgit v1.2.3 From 8db305354698abed7c87b14e4958fb8b688cbfb0 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Fri, 25 Aug 2017 07:46:56 -0400 Subject: Added more on arrays --- vala.html.markdown | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index a8d90e47..a296fe9e 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -67,6 +67,8 @@ stderr.printf("Error message"); // Error printing /* Arrays */ int[] int_array = new int[10]; // Array of ints with 10 slots +int_array.length; // => 10; + int[] int_array2 = {5, 10, 15, 20}; // Can be created on-the-fly int[] array_slice = int_array2[1:3]; // Slice (copy of data) @@ -79,6 +81,14 @@ int[,] multi_array2 = {{7, 4, 6, 4}, {3, 2, 4, 6}, {5, 9, 5, 1}}; multi_array2[2,3] = 12; // 2 is the array, 3 is the index in the array +int first_d = multi_array2.length[0] // => 3 +int second_d = multi_array2.length[1] // => 4 + +// Stacked arrays (e.g. int[][]) where array lengths vary are not supported. + +// Multi-dimensional arrays cannot be sliced, nor can they be converted to one- +// dimensional. + struct Closet { public uint shirts; -- cgit v1.2.3 From 51eaad841657c263f2ec74449d83b956361833a2 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Fri, 25 Aug 2017 08:08:38 -0400 Subject: Finished arrays section and added additional reading on GTK+ GUIs --- vala.html.markdown | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index a296fe9e..c091da05 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -7,7 +7,7 @@ filename: LearnVala.vala In GNOME's own words, "Vala is a programming language that aims to bring modern programming language features to GNOME developers without imposing any additional runtime requirements and without using a different ABI compared to applications and libraries written in C." -Vala has aspects of Java and C#, so it'll be familiar to those who know either or. +Vala has aspects of Java and C#, so it'll be natural to those who know either or. [Read more here.](https://wiki.gnome.org/Projects/Vala) @@ -67,6 +67,7 @@ stderr.printf("Error message"); // Error printing /* Arrays */ int[] int_array = new int[10]; // Array of ints with 10 slots +int better_int_array[10]; // Shorter way of making int array with 10 slots int_array.length; // => 10; int[] int_array2 = {5, 10, 15, 20}; // Can be created on-the-fly @@ -89,6 +90,14 @@ int second_d = multi_array2.length[1] // => 4 // Multi-dimensional arrays cannot be sliced, nor can they be converted to one- // dimensional. +int[] add_to_array = {}; +add_to_array += 12; // Arrays can be dynamically added to + +add_to_array.resize(20); // Array now has 20 slots + +uint8[] chars = "test message".data; +chars.move(5, 0, 7); +print ((string) chars); // Casts the array to a string and prints "message" struct Closet { public uint shirts; @@ -102,3 +111,5 @@ enum HouseSize { } ``` + +* Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw). -- cgit v1.2.3 From 2d70b5123f670d4e33d09e9b87b807788f69e0bb Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Fri, 25 Aug 2017 08:54:54 -0400 Subject: Added control flow section --- vala.html.markdown | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index c091da05..7cc59670 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -99,6 +99,75 @@ uint8[] chars = "test message".data; chars.move(5, 0, 7); print ((string) chars); // Casts the array to a string and prints "message" +/* Control Flow */ + +var a = 1; +var b = 2; +int[] foreach_demo = {2, 4, 6, 8}; + +while (b > a) { // While loop; checks if expression is true before executing + b--; +} + +do { + b--; +} +while (b > a); // Do While loop; executes the code in "do" before while (b > a) + +for (a = 0; a < 10; a++) { stdout.printf("%d\n", a); } // for loop + +foreach (int foreach_demo_var in foreach_demo) { + stdout.printf("%d\n", foreach_demo_var); +} // foreach works on any iterable collection + +if (a == 0) { + break; +} else if (a > 1) { + stdout.printf("%d\n", a); +} else { + break; +} // if-then-else + +switch (a) { + case 1: + stdout.printf("A is 1\n"); + break; + case 5: + case 10: + stdout.printf("A is 5 or 10\n"); + break; + default: + stdout.printf("???\n") + break; +} // switch statement + +/* Reference Types */ + +// Reference types are classes. + +class Message : GLib.Object { // Class Message extends GLib's Object + public string sender; // a public field + public string text {get; set;} // a public property + private bool sent = false; // private field + public void send() { // public method + sent = true; + } + +} + +int cast_to_float = 10; +float casted_float = (float) cast_to_float; // static casting; no runtime checks + +// For runtime checks, use dynamic casting. +// Dynamically casted objects must meet the following: +// - Object's class is the same class as the desired type +// - Object's class is a subclass of the desired type +// - Desired class is an interface implemented by the object's class + +float dyna_casted_float = cast_to_float as float // Won't compile + +var inferred_string = "hello"; // Type inference + struct Closet { public uint shirts; public uint jackets; -- cgit v1.2.3 From 0510b181f0987b516402e63270b062bdecd360f2 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Fri, 25 Aug 2017 09:20:15 -0400 Subject: Added section on functions --- vala.html.markdown | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index 7cc59670..73ffa55c 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -149,7 +149,8 @@ class Message : GLib.Object { // Class Message extends GLib's Object public string sender; // a public field public string text {get; set;} // a public property private bool sent = false; // private field - public void send() { // public method + public void send(string message_sender) { // public method + sender = message_sender; sent = true; } @@ -168,6 +169,54 @@ float dyna_casted_float = cast_to_float as float // Won't compile var inferred_string = "hello"; // Type inference +/* Methods (a.k.a. functions) */ + +// Vala methods are C functions, and are bound by the same rules. + +int method_demo(string arg1, Object arg2) { // Returns int and takes args + return 1; +} + +// Vala methods cannot be overloaded. + +void some_method(string text) { } +void some_method(int number) { } // Won't compile + +// To achieve similar functionality, use default argument values. + +void some_better_method(string text, int number = 0) { } + +// varargs (variable-length argument lists) are also supported. + +void method_with_varargs(int arg1, ...) { + var varargs_list = va_list(); // gets the varargs list + string arg_string = varargs_list.arg(); // gets arguments, one after another + int int_vararg = varargs_list.arg(); + stdout.printf("%s, %d\n", arg_string, int_vararg) +} + +string? ok_to_be_null(int? test_int) { } // "?" denotes possible null value + +// Delegates + +delegate void DelegateDemo(char char_a); + +void delegate_match(char char_a) { // Matches the delegate's signature + stdout.printf("%d\n"); +} + +void call_delegate(DelegateDemo d, char char_b) { // Takes a delegate arg + d(char_b) // calls delegate +} + +void final_delegate_demo() { + call_delegate(delegate_match); // Passes matching method as argument +} + +// Lambdas/Anonymous Methods are defined with "=>" + +(a) => { stdout.printf("%d\n", a); } // Prints "a" + struct Closet { public uint shirts; public uint jackets; -- cgit v1.2.3 From 1abae4b25de43e05df3ba225986997bc72eb3f8a Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Fri, 25 Aug 2017 10:11:24 -0400 Subject: Added namespaces, structs, began classes and OOP, and removed Reference Types. --- vala.html.markdown | 60 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 15 deletions(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index 73ffa55c..036e873d 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -141,20 +141,7 @@ switch (a) { break; } // switch statement -/* Reference Types */ - -// Reference types are classes. - -class Message : GLib.Object { // Class Message extends GLib's Object - public string sender; // a public field - public string text {get; set;} // a public property - private bool sent = false; // private field - public void send(string message_sender) { // public method - sender = message_sender; - sent = true; - } - -} +/* Type Casting and Inference */ int cast_to_float = 10; float casted_float = (float) cast_to_float; // static casting; no runtime checks @@ -217,11 +204,54 @@ void final_delegate_demo() { (a) => { stdout.printf("%d\n", a); } // Prints "a" +/* Namespaces */ + +namespace NamespaceDemo { + // Allows you to organize variable names + int namespace_int = 12; +} +namespace_int += 5; // Won't compile + +using NamespaceDemo; +namespace_int += 5; // Valid + +/* Structs */ + struct Closet { - public uint shirts; + public uint shirts; // Default access modifier is private public uint jackets; } +Closet struct_init_1 = Closet(); // or Closet struct_init_1 = {}; +Closet struct_init_2 = {15, 3}; +var struct_init_3 = Closet() { // Type inference also works + shirts = 15; + jackets = 3; +} + +/* Classes and Object-Oriented Programming */ + +class Message : GLib.Object { // Class Message extends GLib's Object + private string sender; // a private field + public string text {get; set;} // a public property + internal bool sent = false; // internal (classes in same package) field + + public void send(string message_sender) { // public method + sender = message_sender; + sent = true; + } + + public Message() { // Constructor + // ... + } + +} + +interface InterfaceDemo { // Can be used as a mixin + // ... +} + + enum HouseSize { SMALL, MODERATE, -- cgit v1.2.3 From 35aa1410e2becb5d768b9c5142e069b4d0fc46df Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Fri, 25 Aug 2017 10:36:20 -0400 Subject: Finished basic OOP --- vala.html.markdown | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index 036e873d..a65c528f 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -251,6 +251,25 @@ interface InterfaceDemo { // Can be used as a mixin // ... } +// Since method overloading isn't possible, you can use named constructors +// to get the same functionality. + +public class Calculator : GLib.Object { + + public Calculator() { + } + + public Calculator.with_name(string name) { + } + + public Calculator.model(string model_id, string name = "") { + this.with_name(@"$model_id $name"); // Chained constructors with "this" + } + ~Calculator() { } // Only needed if you're using manual memory management +} + +var calc1 = new Calculator.with_name("Temp"); +var calc2 = new Calculator.model("TI-84"); enum HouseSize { SMALL, -- cgit v1.2.3 From 09d8feda849561b1302cc0d53fee787112ae075b Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Fri, 25 Aug 2017 10:38:25 -0400 Subject: Moved enum into structs and renamed section to structs and enums --- vala.html.markdown | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index a65c528f..2711f078 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -215,7 +215,7 @@ namespace_int += 5; // Won't compile using NamespaceDemo; namespace_int += 5; // Valid -/* Structs */ +/* Structs and Enums */ struct Closet { public uint shirts; // Default access modifier is private @@ -229,6 +229,13 @@ var struct_init_3 = Closet() { // Type inference also works jackets = 3; } +enum HouseSize { // An example of an enum + SMALL, + MODERATE, + BIG +} + + /* Classes and Object-Oriented Programming */ class Message : GLib.Object { // Class Message extends GLib's Object @@ -271,12 +278,6 @@ public class Calculator : GLib.Object { var calc1 = new Calculator.with_name("Temp"); var calc2 = new Calculator.model("TI-84"); -enum HouseSize { - SMALL, - MODERATE, - BIG -} - ``` * Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw). -- cgit v1.2.3 From a300e6d19c0139e1e883b0d06facef1ca2f01182 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Fri, 25 Aug 2017 10:39:51 -0400 Subject: Added valadoc.org --- vala.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index 2711f078..4072f3b3 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -279,5 +279,5 @@ var calc1 = new Calculator.with_name("Temp"); var calc2 = new Calculator.model("TI-84"); ``` - +* More Vala documentation can be found [here](https://valadoc.org/). * Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw). -- cgit v1.2.3 From 4857a1342edc69a08ab48f7277cf0ff19b1a7c88 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Fri, 25 Aug 2017 12:07:21 -0400 Subject: General improvements --- vala.html.markdown | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index 4072f3b3..a0fbc5c1 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -7,7 +7,7 @@ filename: LearnVala.vala In GNOME's own words, "Vala is a programming language that aims to bring modern programming language features to GNOME developers without imposing any additional runtime requirements and without using a different ABI compared to applications and libraries written in C." -Vala has aspects of Java and C#, so it'll be natural to those who know either or. +Vala has aspects of Java and C#, so it'll be natural to those who know either. [Read more here.](https://wiki.gnome.org/Projects/Vala) @@ -24,8 +24,6 @@ Comment */ /* Data Types */ -// Vala supports the data types supported by most other programming languages. - char character = 'a' unichar unicode_character = 'u' // 32-bit unicode character @@ -40,10 +38,10 @@ ushort m; string text = "Hello,"; // Note that the == operator will check string content string verbatim = """This is a verbatim (a.k.a. raw) string. Special characters -(e.g. \n) are not interpreted. They may also be multiple lines long."""; +(e.g. \n and "") are not interpreted. They may also be multiple lines long."""; // String Templates allow for easy string formatting -string string_template = @"$text world"; // "$text" evaluates to "Hello" +string string_template = @"$text world"; // "$text" evaluates to "Hello," int test = 5; int test2 = 10; @@ -54,8 +52,8 @@ string template_slice = string_template[7:12]; // => "world" // Most data types have methods for parsing. bool parse_bool = bool.parse("false"); // => false -int parse_int = int.parse("-52"); -string parse_string = parse_int.to_string(); +int parse_int = int.parse("-52"); // => -52 +string parse_string = parse_int.to_string(); // => "-52" /* Basic I/O */ @@ -67,7 +65,7 @@ stderr.printf("Error message"); // Error printing /* Arrays */ int[] int_array = new int[10]; // Array of ints with 10 slots -int better_int_array[10]; // Shorter way of making int array with 10 slots +int better_int_array[10]; // Above expression, shortened int_array.length; // => 10; int[] int_array2 = {5, 10, 15, 20}; // Can be created on-the-fly @@ -80,7 +78,7 @@ unowned int[] array_slice_ref = int_array2[1:3]; // Reference to data int[,] multi_array = new int[6,4]; // 6 is the number of arrays, 4 is their size int[,] multi_array2 = {{7, 4, 6, 4}, {3, 2, 4, 6}, - {5, 9, 5, 1}}; + {5, 9, 5, 1}}; // new int[3,4] multi_array2[2,3] = 12; // 2 is the array, 3 is the index in the array int first_d = multi_array2.length[0] // => 3 int second_d = multi_array2.length[1] // => 4 @@ -97,12 +95,12 @@ add_to_array.resize(20); // Array now has 20 slots uint8[] chars = "test message".data; chars.move(5, 0, 7); -print ((string) chars); // Casts the array to a string and prints "message" +stdout.printf((string) chars); // Casts the array to a string and prints it /* Control Flow */ -var a = 1; -var b = 2; +int a = 1; +int b = 2; int[] foreach_demo = {2, 4, 6, 8}; while (b > a) { // While loop; checks if expression is true before executing @@ -147,7 +145,7 @@ int cast_to_float = 10; float casted_float = (float) cast_to_float; // static casting; no runtime checks // For runtime checks, use dynamic casting. -// Dynamically casted objects must meet the following: +// Dynamically casted objects must be the following: // - Object's class is the same class as the desired type // - Object's class is a subclass of the desired type // - Desired class is an interface implemented by the object's class @@ -158,8 +156,6 @@ var inferred_string = "hello"; // Type inference /* Methods (a.k.a. functions) */ -// Vala methods are C functions, and are bound by the same rules. - int method_demo(string arg1, Object arg2) { // Returns int and takes args return 1; } @@ -173,12 +169,17 @@ void some_method(int number) { } // Won't compile void some_better_method(string text, int number = 0) { } +some_better_method("text"); +some_better_method("text", 12); + // varargs (variable-length argument lists) are also supported. void method_with_varargs(int arg1, ...) { var varargs_list = va_list(); // gets the varargs list + string arg_string = varargs_list.arg(); // gets arguments, one after another int int_vararg = varargs_list.arg(); + stdout.printf("%s, %d\n", arg_string, int_vararg) } @@ -200,7 +201,7 @@ void final_delegate_demo() { call_delegate(delegate_match); // Passes matching method as argument } -// Lambdas/Anonymous Methods are defined with "=>" +// Lambdas (a.k.a. Anonymous Methods) are defined with "=>" (a) => { stdout.printf("%d\n", a); } // Prints "a" @@ -235,13 +236,13 @@ enum HouseSize { // An example of an enum BIG } - /* Classes and Object-Oriented Programming */ class Message : GLib.Object { // Class Message extends GLib's Object private string sender; // a private field public string text {get; set;} // a public property - internal bool sent = false; // internal (classes in same package) field + protected bool is_digital = true; // protected (this class and subclasses) + internal bool sent = false; // internal (classes in same package) public void send(string message_sender) { // public method sender = message_sender; -- cgit v1.2.3 From 81f58f4b0d101014fa1df81e2f48e123dd0dbcf2 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Fri, 25 Aug 2017 12:26:44 -0400 Subject: Added section on Signals --- vala.html.markdown | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index a0fbc5c1..237d4a15 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -279,6 +279,29 @@ public class Calculator : GLib.Object { var calc1 = new Calculator.with_name("Temp"); var calc2 = new Calculator.model("TI-84"); +// Signals (a.k.a. events or event listeners) are a way to execute multiple +// methods with the same signature at the same time. + +public class SignalDemo : GLib.Object { + public signal void sig_demo(int sig_demo_int); // Must be public + + public static int main(string[] args) { + // main method; program does not compile without it + var sig_demo_class = new SignalDemo(); // New instance of class + + sig_demo_class.sig_demo.connect((ob, sig_int) => { // Lambda used as handler + stdout.printf("%d\n", sig_int); // "ob" is object on which it is emitted + }); + + sig_demo_class.sig_demo(27); // Signal is emitted + + return 0; + } +} + +// You may use the connect() method and attach as many handlers as you'd like. +// They'll all run when the signal is emitted. + ``` * More Vala documentation can be found [here](https://valadoc.org/). * Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw). -- cgit v1.2.3 From fe5001ca964e048ea92f0f7b15f486e57bfb5235 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Fri, 25 Aug 2017 12:33:39 -0400 Subject: Added properties (get and set) --- vala.html.markdown | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index 237d4a15..f0141eb4 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -240,7 +240,7 @@ enum HouseSize { // An example of an enum class Message : GLib.Object { // Class Message extends GLib's Object private string sender; // a private field - public string text {get; set;} // a public property + public string text {get; set;} // a public property (more on that later) protected bool is_digital = true; // protected (this class and subclasses) internal bool sent = false; // internal (classes in same package) @@ -302,6 +302,24 @@ public class SignalDemo : GLib.Object { // You may use the connect() method and attach as many handlers as you'd like. // They'll all run when the signal is emitted. +// Properties (getters and setters) + +class Animal : GLib.Object { + private int _legs; // prefixed with underscore to prevents name clashes + + public int legs { + get { return _legs; } + set { _legs = value; } + } + + public int eyes { get; set; default = 5; } // Shorter way +} + +rabbit = new Animal(); +rabbit.legs = 2; +rabbit.legs += 2; +rabbit.eyes = 2; + ``` * More Vala documentation can be found [here](https://valadoc.org/). * Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw). -- cgit v1.2.3 From f560802e77202d249c024678308ee422480df0cb Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Fri, 25 Aug 2017 13:03:09 -0400 Subject: Added more to Properties section --- vala.html.markdown | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index f0141eb4..b87e16e6 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -313,12 +313,26 @@ class Animal : GLib.Object { } public int eyes { get; set; default = 5; } // Shorter way -} + public int kingdom { get; private set; default = "Animalia"} // Read-only + + public static void main(string args[]) { + rabbit = new Animal(); + + // All GLib.Objects have a signal "notify" emitted when a property changes. + + // If you specify a specific property, replace all underscores with dashes + // to conform with the GObject naming convention. -rabbit = new Animal(); -rabbit.legs = 2; -rabbit.legs += 2; -rabbit.eyes = 2; + rabbit.notify["eyes"].connect((s, p) => { // Remove the ["eyes"] for all + stdout.printf("Property '%s' has changed!\n", p.name); + }); + + rabbit.legs = 2; + rabbit.legs += 2; + rabbit.eyes = 2; + + } +} ``` * More Vala documentation can be found [here](https://valadoc.org/). -- cgit v1.2.3 From bf7ed153ee023ea2ba529ccf4ee486b1a59a183a Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Fri, 25 Aug 2017 13:14:24 -0400 Subject: Added inheritance --- vala.html.markdown | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index b87e16e6..11165aa4 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -244,8 +244,8 @@ class Message : GLib.Object { // Class Message extends GLib's Object protected bool is_digital = true; // protected (this class and subclasses) internal bool sent = false; // internal (classes in same package) - public void send(string message_sender) { // public method - sender = message_sender; + public void send(string sender) { // public method + this.sender = sender; sent = true; } @@ -334,6 +334,25 @@ class Animal : GLib.Object { } } +// Inheritance: Vala classes may inherit 1 class. Inheritance is not implicit. + +class SuperDemo : GLib.Object { + public int data1; + protected int data2; + internal int data3; + private int data4; + + public static void test_method { } // Statics can be called w/out an object +} +class SubDemo : SuperDemo { + public static void main(string args[]) { + stdout.printf((string) data1); // Will compile + stdout.printf((string) data2); // Protected can be accessed by subclasses + stdout.printf((string) data3); // Internal is accessible to package + stdout.printf((string) data4); // Won't compile + } +} + ``` * More Vala documentation can be found [here](https://valadoc.org/). * Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw). -- cgit v1.2.3 From a6359357fdc0cec3731a5f3088bb8d590b9cc744 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Fri, 25 Aug 2017 13:24:01 -0400 Subject: Added section on abstract classes/methods --- vala.html.markdown | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index 11165aa4..3103f9c5 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -353,6 +353,34 @@ class SubDemo : SuperDemo { } } +// Abstract Classes and Methods + +public abstract class OperatingSystem : GLib.Object { + public void turn_on() { + stdout.printf("Booted successfully.\n"); + } + public abstract void use_computer(); +} + +public class Ubuntu : OperatingSystem { + public override void use_computer() { // Abstract methods must be overridden + stdout.printf("Beep boop\n"); + } +} + +// Add default behavior to an abstract method by making it "virtual". + +public abstract class HardDrive : GLib.Object { + public virtual void die() { + stdout.printf("CLICK-CLICK-CLICK\n"); + } +} +public class MyHD : HardDrive { + public override void die() { + return; + } +} + ``` * More Vala documentation can be found [here](https://valadoc.org/). * Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw). -- cgit v1.2.3 From 82a37a456ab14869a0d78c859c2acc22bee06b18 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Sat, 26 Aug 2017 08:30:30 -0400 Subject: removed break statements from if-then-else --- vala.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index 3103f9c5..91059a92 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -119,11 +119,11 @@ foreach (int foreach_demo_var in foreach_demo) { } // foreach works on any iterable collection if (a == 0) { - break; + stdout.printf("%d\n", a); } else if (a > 1) { stdout.printf("%d\n", a); } else { - break; + stdout.printf("A is less than 0"); } // if-then-else switch (a) { -- cgit v1.2.3 From b86786023cce9b664dc7f529e1c7bea47356b8e3 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Sat, 26 Aug 2017 08:49:38 -0400 Subject: Clarifications in delegates --- vala.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index 91059a92..130ce7a9 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -189,7 +189,7 @@ string? ok_to_be_null(int? test_int) { } // "?" denotes possible null value delegate void DelegateDemo(char char_a); -void delegate_match(char char_a) { // Matches the delegate's signature +void delegate_match(char char_a) { // Matches DelegateDemo's signature stdout.printf("%d\n"); } -- cgit v1.2.3 From 14c3fc4ffe96ec9c13469a6005ce4e1497c74358 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Sat, 26 Aug 2017 08:57:34 -0400 Subject: fixed interface --- vala.html.markdown | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index 130ce7a9..ac1f19eb 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -255,8 +255,9 @@ class Message : GLib.Object { // Class Message extends GLib's Object } -interface InterfaceDemo { // Can be used as a mixin - // ... +interface Laptop { // May only contain abstract methods + public abstract void turn_on(); + public abstract void turn_off(); } // Since method overloading isn't possible, you can use named constructors -- cgit v1.2.3 From f6de93559f8d2b725d307675d4b333b37a2fd6bc Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Sat, 26 Aug 2017 09:01:25 -0400 Subject: General spelling/grammer fixes --- vala.html.markdown | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index ac1f19eb..a7fd1131 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -288,6 +288,7 @@ public class SignalDemo : GLib.Object { public static int main(string[] args) { // main method; program does not compile without it + var sig_demo_class = new SignalDemo(); // New instance of class sig_demo_class.sig_demo.connect((ob, sig_int) => { // Lambda used as handler @@ -301,12 +302,12 @@ public class SignalDemo : GLib.Object { } // You may use the connect() method and attach as many handlers as you'd like. -// They'll all run when the signal is emitted. +// They'll all run at around the same time when the signal is emitted. // Properties (getters and setters) class Animal : GLib.Object { - private int _legs; // prefixed with underscore to prevents name clashes + private int _legs; // prefixed with underscore to prevent name clashes public int legs { get { return _legs; } -- cgit v1.2.3 From 68951045a37e01b677510cd44386850a69310325 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Sat, 26 Aug 2017 09:05:24 -0400 Subject: Small improvements --- vala.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index a7fd1131..6e1a215f 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -323,7 +323,7 @@ class Animal : GLib.Object { // All GLib.Objects have a signal "notify" emitted when a property changes. // If you specify a specific property, replace all underscores with dashes - // to conform with the GObject naming convention. + // to conform to the GObject naming convention. rabbit.notify["eyes"].connect((s, p) => { // Remove the ["eyes"] for all stdout.printf("Property '%s' has changed!\n", p.name); -- cgit v1.2.3 From ce745f43ed3deef655b232cb095e3d446db716a5 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Sat, 26 Aug 2017 09:09:15 -0400 Subject: Moved interface into its own section and clarified constructor overloading --- vala.html.markdown | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index 6e1a215f..923b152e 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -255,13 +255,8 @@ class Message : GLib.Object { // Class Message extends GLib's Object } -interface Laptop { // May only contain abstract methods - public abstract void turn_on(); - public abstract void turn_off(); -} - -// Since method overloading isn't possible, you can use named constructors -// to get the same functionality. +// Since method overloading isn't possible, you can't overload constructors. +// However, you can use named constructors to achieve the same functionality. public class Calculator : GLib.Object { @@ -383,6 +378,14 @@ public class MyHD : HardDrive { } } +// Interfaces: classes can implement any number of these. + +interface Laptop { // May only contain abstract methods + public abstract void turn_on(); + public abstract void turn_off(); +} + + ``` * More Vala documentation can be found [here](https://valadoc.org/). * Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw). -- cgit v1.2.3 From 41e5f956e1c3b351c85466d00c9f89d05ca7a84d Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Sat, 26 Aug 2017 10:34:16 -0400 Subject: Began interface section --- vala.html.markdown | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index 923b152e..c329447d 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -380,11 +380,26 @@ public class MyHD : HardDrive { // Interfaces: classes can implement any number of these. -interface Laptop { // May only contain abstract methods +interface Laptop { // May only contain abstracts or virtuals public abstract void turn_on(); public abstract void turn_off(); + + public abstract int cores; // Won't compile; fields cannot be abstract + public abstract int cores {get; set;} // Will compile + + public virtual void keyboard() { // Virtuals are allowed (unlike Java/C#) + stdout.printf("Clickity-clack\n"); + } } +// The ability to use virtuals in Vala means that multiple inheritance is +// possible (albeit somewhat confined) + +// Interfaces cannot implement interfaces, but they may specify that certain +// interfaces or classes must be also implemented (pre-requisites). + +public interface CellPhone : Collection, GLib.Object {} + ``` * More Vala documentation can be found [here](https://valadoc.org/). -- cgit v1.2.3 From 1b780b2d46bfad3084df5e454d5b5ca81601595a Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Sat, 26 Aug 2017 10:46:54 -0400 Subject: Getting type info dynamically --- vala.html.markdown | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index c329447d..51cb8dc0 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -359,7 +359,7 @@ public abstract class OperatingSystem : GLib.Object { public abstract void use_computer(); } -public class Ubuntu : OperatingSystem { +public class Linux : OperatingSystem { public override void use_computer() { // Abstract methods must be overridden stdout.printf("Beep boop\n"); } @@ -400,6 +400,15 @@ interface Laptop { // May only contain abstracts or virtuals public interface CellPhone : Collection, GLib.Object {} +// You can get the type info of a class at runtime dynamically. + +bool type_info = object is TypeName; // uses "is" to get a bool + +Type type_info2 = object.get_type(); +var type_name = type_info2.name(); + +Type type_info3 = typeof(Linux); +Linux type_demo = (Linux) Object.new(type_info3); ``` * More Vala documentation can be found [here](https://valadoc.org/). -- cgit v1.2.3 From a0095c2884580f5a479c677f08470c5aee4a7555 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Sat, 26 Aug 2017 10:58:00 -0400 Subject: Added section on generics --- vala.html.markdown | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index 51cb8dc0..350d3d88 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -410,6 +410,21 @@ var type_name = type_info2.name(); Type type_info3 = typeof(Linux); Linux type_demo = (Linux) Object.new(type_info3); +// Generics + +class Computer : GLib.Object { + private OperatingSystem os; + + public void install_os(OperatingSystem os) { + this.os = os; + } + public OperatingSystem retrieve_os() { + return this.os; + } +} + +var new_computer = new Computer(); + ``` * More Vala documentation can be found [here](https://valadoc.org/). * Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw). -- cgit v1.2.3 From 07431ea0d249fedda5ab6dcfcbb825dff6c2c63d Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Sat, 26 Aug 2017 10:59:18 -0400 Subject: Added reading on GObject style construction --- vala.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index 350d3d88..b9ec558d 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -427,4 +427,5 @@ var new_computer = new Computer(); ``` * More Vala documentation can be found [here](https://valadoc.org/). +* [Alternate construction syntax](https://wiki.gnome.org/Projects/Vala/Tutorial#GObject-Style_Construction) similar to GObject * Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw). -- cgit v1.2.3 From 374ce84fe9f672b49192ddff2f86d5e3f98d8491 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Sat, 26 Aug 2017 11:11:22 -0400 Subject: Added other features: assertions & contract programming & error handling --- vala.html.markdown | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index b9ec558d..93ae6abd 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -425,7 +425,38 @@ class Computer : GLib.Object { var new_computer = new Computer(); +/* Other Features */ + +// Assertions: crash if a statement is not true (at runtime) + +bool is_true = true; +assert(is_true); + +// Contract Programming + +int contract_demo(int arg1, int arg2) { + requires(arg1 > 0 && arg1 < 10) // Notice the lack of semicolon + requires(arg2 >= 12) + ensures(result >= 0) +} + +// Error Handling + +void error_demo(int int_ex) throws GError { + if (int_ex != 1) { + throw new GError("TEST MESSAGE"); + } +} +void error_demo2() { + try { + error_demo(0); + } catch (GError ge) { + stdout.printf("%s\n", ge.message); + } +} + ``` * More Vala documentation can be found [here](https://valadoc.org/). * [Alternate construction syntax](https://wiki.gnome.org/Projects/Vala/Tutorial#GObject-Style_Construction) similar to GObject +* More on contract programming [here](http://en.wikipedia.org/wiki/Contract_programming) * Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw). -- cgit v1.2.3 From 0fa0305aaeec6b94274a0f7f94f52cf407ac2575 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Sat, 26 Aug 2017 11:25:20 -0400 Subject: Added main loop section and collections library/multithreading info --- vala.html.markdown | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index 93ae6abd..5ff3486c 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -455,8 +455,30 @@ void error_demo2() { } } +// Main Loop + +void main() { + + var main_loop = new MainLoop(); + var time = new TimeoutSource(2000); + + time.set_callback(() => { // Executes the following lambda after 2000ms + stdout.printf("2000ms have passed\n"); + main_loop.quit(); + return false; + }); + + time.attach(main_loop.get_context()); + + loop.run(); +} + +// Pointers + ``` * More Vala documentation can be found [here](https://valadoc.org/). * [Alternate construction syntax](https://wiki.gnome.org/Projects/Vala/Tutorial#GObject-Style_Construction) similar to GObject * More on contract programming [here](http://en.wikipedia.org/wiki/Contract_programming) +* Collections library can be found [here](https://wiki.gnome.org/Projects/Vala/Tutorial#Collections) +* [Multithreading](https://wiki.gnome.org/Projects/Vala/Tutorial#Multi-Threading) * Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw). -- cgit v1.2.3 From 5eb6c15162a1a15507fb20497a02d17a7d6a926a Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Sat, 26 Aug 2017 11:30:06 -0400 Subject: added pointers section --- vala.html.markdown | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index 5ff3486c..9a48aee9 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -473,7 +473,18 @@ void main() { loop.run(); } -// Pointers +// Pointers (manual memory management) + +Object* pointer_obj = new Object(); // Creates Object instance and gives pointer + +pointer_obj->some_method(); // Executes some_method +pointer_obj->some_data; // Returns some_data + +delete pointer_obj; + +int more = 57; +int* more_pointer = &i; // & = address-of +int indirection_demo = more_pointer*; // indirection ``` * More Vala documentation can be found [here](https://valadoc.org/). -- cgit v1.2.3 From f5e226ae8a70917a3272f10b9f35b5eced085a04 Mon Sep 17 00:00:00 2001 From: Milo Gilad Date: Sat, 26 Aug 2017 11:33:08 -0400 Subject: Added profiles and D-Bus integration reading --- vala.html.markdown | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'vala.html.markdown') diff --git a/vala.html.markdown b/vala.html.markdown index 9a48aee9..393578b0 100755 --- a/vala.html.markdown +++ b/vala.html.markdown @@ -486,6 +486,13 @@ int more = 57; int* more_pointer = &i; // & = address-of int indirection_demo = more_pointer*; // indirection +// Profiles: affect which Vala features are avaliable and which libraries the +// C-code will use. +// - gobject (default) +// posix +// dova +// Use "--profile=whatever" when compiling. + ``` * More Vala documentation can be found [here](https://valadoc.org/). * [Alternate construction syntax](https://wiki.gnome.org/Projects/Vala/Tutorial#GObject-Style_Construction) similar to GObject @@ -493,3 +500,4 @@ int indirection_demo = more_pointer*; // indirection * Collections library can be found [here](https://wiki.gnome.org/Projects/Vala/Tutorial#Collections) * [Multithreading](https://wiki.gnome.org/Projects/Vala/Tutorial#Multi-Threading) * Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw). +* D-Bus [integration](https://wiki.gnome.org/Projects/Vala/Tutorial#D-Bus_Integration) -- cgit v1.2.3