Values - cyxigo/wi GitHub Wiki
Introduction
Values are pieces of data in programming languages. They can be several types in Wi: real, null, bool, string, array, map, function, foreign, object, userdata, and modules.
Let's go through each one.
Real
A real number is any number. In Wi's implementation, it uses double-precision floating point format.
0
1234
0.4321
-124
-0.421
3.14159265359
1.0
Wi also supports other numeric notations such as:
0xcafebabe %% hex!
0b01010101 %% binary!
0o07423124 %% octal, even! ...who uses octal?
1e+9 %% scientific!
Null
null represents nothing – an absence of a value.
null
Bool
Bool (short for boolean) can store only two values: true or false. They are used to indicate whether a condition is truthy or falsy.
true
false
String
A string is an immutable array of characters. Once created, you can access its characters via [], but you cannot mutate it.
"Hello World!"
"I love Wi"
"Meow"
Accessing characters:
"Hello"[0]; %% H
"Hello"[4]; %% o
[!IMPORTANT] All string functions in Wi's standard method library return a new string – they never modify the original.
Escape sequences
In programming, an escape sequence is a combination of characters in a string that starts with a backslash and converts into a special or non-printable character. For example, a newline! How are you going to write a newline? You use escape sequence \n!
All supported escape sequences are:
"\r" %% carriage return
"\n" %% newline
"\t" %% horizontal tab
"\v" %% vertical tab
"\b" %% backspace
"\a" %% alarm beep (...alarm beep? really?)
"\f" %% formfeed
"\\" %% actual backslash
"\"" %% double quotes inside a string
"\0" %% NUL-byte – printing a string with one shows a space in its place, not the byte itself
"\$" %% $ is reserved for string interpolation, so this allows you to use it without starting an interpolation
String interpolation
You can embed expressions directly inside a string with ${ }:
x := 10;
std::puts("x is ${x} and x + 1 is ${x + 1}"); %% x is 10 and x + 1 is 11
That's called string interpolation.
Interpolation can be nested (an interpolated expression can itself contain a string with interpolation) up to a depth of 8.
Raw strings
You can also write a raw string using backticks (`) instead of double quotes. Raw strings don't process escape sequences (like \n), don't process interpolations, and can span multiple lines:
`C:\Users\no need to escape backslashes here`;
Array
An array is a linear collection of data. Items are accessed via [i], where i is the index (0-based).
Arrays can be mutated:
a := [1, 2, 3];
a[0] = 10; %% [10, 2, 3]
Or using built-in methods:
a := [1, 2, 3];
a->add(10); %% [1, 2, 3, 10]
a->remove(2); %% [1, 3, 10]
Accessing an index out of range will cause a runtime error:
a := [];
a[0];
runtime error: array index out of range: 0
--> file.wi:1 in main function
Map
Map is a non-linear collection of key-value pairs. Access values with [key]:
m := { "x": 10 };
std::puts(m["x"]); %% 10
m["y"] = 20;
std::puts(m["y"]); %% 20
Accessing a key that does not exist will cause a runtime error:
m := {};
std::puts(m["x"]);
runtime error: map has no key x
--> file.wi:1 in main function
Using NaN as a key (e.g. from 0 / 0) will cause a runtime error, since NaN never equals itself and could never be looked up again:
m := {};
m[0 / 0] = 10;
runtime error: cannot use NaN as a map key
--> file.wi:2 in main function
More complex values
They are function, foreign, object, userdata, and modules. They all have their respective page because there's way more to talk about.
Next: Variables