2017-08-24 01:52:53 +03:00
---
language: vala
contributors:
2017-08-24 23:10:16 +03:00
- ["Milo Gilad", "https://github.com/Myl0g"]
2017-08-24 01:52:53 +03:00
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."
2017-08-25 15:08:38 +03:00
Vala has aspects of Java and C#, so it'll be natural to those who know either or.
2017-08-24 01:52:53 +03:00
[Read more here. ](https://wiki.gnome.org/Projects/Vala )
```vala
2017-08-24 23:10:16 +03:00
2017-08-24 01:52:53 +03:00
// Single line comment
/* Multiline
Comment */
/**
* Documentation comment
*/
2017-08-24 23:10:16 +03:00
/* Data Types */
2017-08-24 22:54:48 +03:00
// Vala supports the data types supported by most other programming languages.
char character = 'a'
unichar unicode_character = 'u' // 32-bit unicode character
2017-08-24 23:10:16 +03:00
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
2017-08-24 22:54:48 +03:00
long k;
short l;
ushort m;
2017-08-24 23:10:16 +03:00
string text = "Hello,"; // Note that the == operator will check string content
2017-08-24 22:54:48 +03:00
2017-08-24 23:10:16 +03:00
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.""";
2017-08-24 22:54:48 +03:00
2017-08-24 23:10:16 +03:00
// 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
2017-08-25 15:08:38 +03:00
int better_int_array[10]; // Shorter way of making int array with 10 slots
2017-08-25 14:46:56 +03:00
int_array.length; // => 10;
2017-08-24 23:10:16 +03:00
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
2017-08-25 14:46:56 +03:00
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.
2017-08-25 15:08:38 +03:00
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"
2017-08-24 22:54:48 +03:00
2017-08-25 15:54:54 +03:00
/* 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
2017-08-24 23:10:16 +03:00
struct Closet {
public uint shirts;
public uint jackets;
}
2017-08-24 22:54:48 +03:00
2017-08-24 23:10:16 +03:00
enum HouseSize {
SMALL,
MODERATE,
BIG
}
2017-08-24 01:52:53 +03:00
2017-08-24 22:54:48 +03:00
```
2017-08-25 15:08:38 +03:00
* Read about building GUIs with GTK+ and Vala [here ](http://archive.is/7C7bw ).