Lesson 02 of 16

Variables & Types

let, int / float / string / bool / null

download this lesson (.md) ↓ all lessons

2. Variables & Types

Declaring variables

Use let to create a variable. Reassign without let.

let name = "Ember";
let age = 20;
age = 21;            // reassign
print(name, age);    // Ember 21

mut and var are accepted as synonyms of let:

mut score = 0;
score = score + 10;

Types

STencil is dynamically typed. The core types:

Type Example Notes
int 42 whole numbers
float 3.14 decimals
string "hello" text (single or double quotes)
bool true / false
null null "no value"
list [1, 2, 3] see lesson 6
dict { "k": 1 } see lesson 6
object User!(...), Account(...) see lessons 7 & 8

Check a type with type(x):

print(type(42));      // int
print(type(3.14));    // float
print(type("hi"));    // string
print(type(true));    // bool
print(type([1,2]));   // list

Optional type annotations

You may annotate a variable's type; it is currently informational (not enforced):

let count: int = 5;
let pi: float = 3.14159;

Numbers: int vs float

print(7 / 2);     // 3    integer division (both ints)
print(7.0 / 2);   // 3.5  float division (one side is float)
print(3 + 1.5);   // 4.5  mixing promotes to float

Whole-number floats print with a trailing .0:

print(4.0);   // 4.0
print(4);     // 4

Number literals

let dec = 255;        // decimal
let hex = 0xFF;       // hexadecimal -> 255 (handy for colors: 0x101828)
let f = 3.14;         // float
print(dec == hex);    // true

Converting between types

print(int("42") + 1);     // 43
print(float("3.14"));     // 3.14
print(str(100) + "%");    // 100%
print(bool(0), bool(1));  // false true

Next: Operators & Strings