mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-01-13 12:45:58 +00:00
Added arrays section
This commit is contained in:
parent
ca5d90eb7d
commit
0c105ca0b2
@ -1,7 +1,7 @@
|
|||||||
---
|
---
|
||||||
language: vala
|
language: vala
|
||||||
contributors:
|
contributors:
|
||||||
- ["Milo Gilad", "https://github.com/bigbash"]
|
- ["Milo Gilad", "https://github.com/Myl0g"]
|
||||||
filename: LearnVala.vala
|
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)
|
[Read more here.](https://wiki.gnome.org/Projects/Vala)
|
||||||
|
|
||||||
```vala
|
```vala
|
||||||
|
|
||||||
// Single line comment
|
// Single line comment
|
||||||
|
|
||||||
/* Multiline
|
/* Multiline
|
||||||
@ -21,31 +22,73 @@ Comment */
|
|||||||
* Documentation comment
|
* Documentation comment
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/* Data Types */
|
||||||
Data Types
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Vala supports the data types supported by most other programming languages.
|
// Vala supports the data types supported by most other programming languages.
|
||||||
|
|
||||||
char character = 'a'
|
char character = 'a'
|
||||||
unichar unicode_character = 'u' // 32-bit unicode character
|
unichar unicode_character = 'u' // 32-bit unicode character
|
||||||
|
|
||||||
int i; // ints can also have guaranteed sizes (e.g. int64, uint64)
|
int i = 2; // ints can also have guaranteed sizes (e.g. int64, uint64)
|
||||||
uint j;
|
uint j = -6; // Won't compile; unsigned ints can only be positive
|
||||||
|
|
||||||
long k;
|
long k;
|
||||||
|
|
||||||
short l;
|
short l;
|
||||||
ushort m;
|
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.""";
|
||||||
|
|
||||||
/*
|
// String Templates allow for easy string formatting
|
||||||
Basic Syntax
|
string string_template = @"$text world"; // "$text" evaluates to "Hello"
|
||||||
*/
|
|
||||||
|
|
||||||
// Like in C#, scope is defined using braces.
|
int test = 5;
|
||||||
// An object or reference is only valid between braces.
|
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
|
||||||
|
|
||||||
|
struct Closet {
|
||||||
|
public uint shirts;
|
||||||
|
public uint jackets;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum HouseSize {
|
||||||
|
SMALL,
|
||||||
|
MODERATE,
|
||||||
|
BIG
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user