Variables and Literals - mfichman/jogo GitHub Wiki

Numbers

Jogo has very standard syntax for arithmetic:

1 + 2 * 8

The following operators work with numbers: +, -, *, /, and ~ (negation). All integer literals are of type Int, a type that is 64 bits wide. All decimal literals are of type Float, which is always a 64-bit floating point number. Other types of numbers are also available:

Type Precision Signed?
S8 8 bits fixed yes
S16 16 bits fixed yes
S32 32 bits fixed yes
S64 64 bits fixed yes
U8 8 bits fixed no
U16 16 bits fixed no
U32 32 bits fixed no
U64 64 bits fixed no
F32 32 bit float yes
F64 64 bit float yes
Decimal infinite yes

Jogo does not automatically convert between different integer and floating point numbers. To declare a number that isn't an Int or Float, the constructor for the type must be called:

S8(9) + S8(9)

It's also worth noting that all numeric types are also objects, albeit optimized objects called value-types. The programmer can use methods or free functions to perform operations on numbers, like so:

7.sin() + cos(10)

The 9.sin() syntax is a result of the way Jogo treats methods. Methods will be discussed in detail in Section 5.

Strings

Strings also work as one might expect. The following expression would concatenate two string literals:

"trans" + "mogrify"

All strings are immutable, so there are no operations that can change a string after it is constructed. Instead, strings can be manipulated using substring:

"hello".slice(0, 2)

This expression results in the string "he".

Variables

Declaring a variable is quite natural. All three lines below declare variables of type Int:

x = 42
y Int
z Int = 42

Notice that the "Int" is not required; the Jogo compiler can infer the type of x by looking at the right-hand side of the "=" operator. In general, the compiler can always infer the types of variables and attributes, but never infers the types of function parameters. We will discuss attributes and parameters later.

There is also a slight difference between declaring a variable without a type (x = 42) and declaring one with a type (y Int). For the latter only, it is an error if a variable named y is already defined in the current scope (block of code).

⚠️ **GitHub.com Fallback** ⚠️