Skip to content

Basics

This page covers the pieces every Tessel program is made of: comments, printing, constants and variables, literal values, and operators. If you have written code in Swift, Kotlin, JavaScript or a similar language, most of it will look familiar.

// Say hello, then do a little arithmetic.
fn main() {
let name = "Tessel"
print("Hello, {name}!")
var total = 0
for i in 0..5 {
total += i
}
print("The sum of 0..5 is {total}")
}

Run it with tessel run hello.tsl, and it prints:

Hello, Tessel!
The sum of 0..5 is 10

A program without a window starts at fn main(). It takes no parameters and returns nothing. (A program with a window starts at an app instead; see Programs and files.)

print writes one value to the terminal, followed by a new line. It accepts an Int, Float, Bool, String or an enum value. To print several values, put them in one string with {…}, as shown below.

A comment starts with // and runs to the end of the line. It can be on its own line or after code:

// This whole line is a comment.
let width = 20 // and this is a comment after some code

There are no block comments (/* … */). To comment out several lines, put // in front of each one (in the IDE, select them and press Cmd+/).

Comment lines written just above a declaration (a function, a method, a struct, an enum, a view) are its documentation. Editors show them when you hover the name, wherever it’s used: the Tessel IDE and VS Code both do.

// The price of all the items, with tax added.
// `tax` is a fraction: 0.12 for 12%.
fn total(_ prices: [Float], tax: Float) -> Float {
var sum = 0.0
for p in prices {
sum += p
}
sum * (1.0 + tax)
}

Hovering total shows its first line and those two comment lines. Only the comment lines right above the declaration count: a blank line between them and the declaration leaves them out. Write them with //, as any comment; say what the declaration is for and anything a caller should know.

Use let for a value that never changes, and var for one that does:

let name = "Ada" // a constant
var score = 0 // a variable
score = 10
score += 5
print("{name} has {score} points")

Every let and var needs a starting value. Tessel works out the type from that value, so score above is an Int. You can also write the type yourself after a colon:

let ratio: Float = 3 // 3 is read as 3.0 here
var names: [String] = [] // an empty list needs its type written out

Changing a let is an error that tells you to use var instead:

let limit = 10
limit = 20
error: can't change `limit`, which is a constant
= help: declare it with `var` to be able to change it

Variables live inside functions (and state lives inside views). A let can also go at the top level of a file, outside any function. That makes a constant the whole program can use:

let maxPlayers = 4
let colors = ["red", "green", "blue"]
let greeting: String = "Welcome"
let boardCells = maxPlayers * 16
fn main() {
print(maxPlayers)
print(colors.count)
print("{greeting}: {boardCells} cells")
}
4
3
Welcome: 64 cells

A constant’s value can be any expression, including a call to one of your functions, and it can use other constants (in any order, as long as two don’t need each other). Each is worked out once, when the program starts, before main or the app runs.

Only let is allowed there. There are no global variables: a var at the top level of a file is an error, and a constant can’t be changed.

error: can't change `maxPlayers`, which is a constant
= help: a constant keeps its value; values that change go in `state` (in a view or app) or in a `var` (in a function)

Values that change belong to the app, as state.

Names are made of letters, digits and _, and can’t start with a digit. The usual style is lowerCamelCase for variables and functions, and UpperCamelCase for types and views.

KindExamplesType
Whole numbers0, 42, -7, 1_000_000, 0xff, 0b1010, 0o17Int
Decimal numbers3.14, 0.5, 2.0, 1_000.25, 1e6, 2.5e-3Float
Truth valuestrue, falseBool
Text"hello", "Total: {n}"String
No valuenilan optional, see Optionals
Lists[1, 2, 3], [][Int]
Dictionaries["a": 1], [:][String: Int]

You can put _ anywhere in a number to make it easier to read: 1_000_000 is one million.

A decimal number needs digits on both sides of the point: write 0.5, not .5, and 2.0, not 2..

A number with an exponent is a Float: 1e6 is 1000000.0, 2.5e-3 is 0.0025 and 1E+9 is 1000000000.0 (e or E, then an optional sign).

Whole numbers can also be written in hexadecimal (0x), binary (0b) or octal (0o), which is handy for bytes, colors and bit operations. The prefix is lowercase; the digits can be either case:

print(0xff) // 255
print(0xFF_FF) // 65535
print(0b1010) // 10
print(0o17) // 15
print(1e6) // 1000000.0
print(2.5e-3) // 0.0025

A whole-number literal takes its type from where it is used. Where a Float is expected, 3 means 3.0, so you rarely have to write .0:

let width: Float = 3
print(width) // 3.0
print(1 + 2.5) // 3.5

This only applies to literals. A variable never changes type by itself; see Types for converting between Int and Float.

A string is text between double quotes. It must end on the same line it starts on.

Put any expression inside { and } to insert its value into the text:

let apples = 3
let price = 2
print("{apples} apples cost {apples * price} coins")
3 apples cost 6 coins

You can interpolate an Int, Float, Bool, String, Range or enum value. Lists, structs and optionals can’t go in directly; for example, use {list.count}, {names.joined(separator: ", ")} or {nickname ?? "none"} instead.

A backslash starts an escape sequence:

EscapeMeans
\nnew line
\ttab
\rcarriage return
\"a double quote
\\a backslash
\{ and \}a literal brace, not an interpolation
print("Line one\nLine two")
print("She said \"hi\"")
print("Path: C:\\temp")
print("Use \{name\} to insert a value")
Line one
Line two
She said "hi"
Path: C:\temp
Use {name} to insert a value

Any other character after a backslash is an error.

OperatorMeaningExampleResult
+add7 + 29
-subtract7 - 25
*multiply7 * 214
/divide7 / 23
%remainder7 % 21
-xnegate-7-7

Both sides must have the same type. Dividing two Ints gives an Int and drops the fraction (it rounds toward zero, so -7 / 2 is -3). For a decimal result, convert first: Float(7) / 2 is 3.5. % works on Floats too: it’s what is left after taking away as many whole multiples of the right side as fit, with the sign of the left side (like Swift’s truncatingRemainder and % in Rust or C):

print(7.5 % 2.0) // 1.5
print(-7.5 % 2.0) // -1.5
print(370.0 % 360.0) // 10.0

+ also joins two strings or two lists:

let greeting = "Hello, " + "world"
let numbers = [1, 2] + [3]

Int arithmetic is checked. If a result doesn’t fit in an Int, or you divide an Int by zero, the program stops with an error; see Runtime errors.

OperatorMeaning
==equal
!=not equal
< <=less than, less than or equal
> >=greater than, greater than or equal

== and != work on almost every type, including strings, lists, structs, enums and optionals. <, <=, > and >= work on numbers and strings (strings compare in dictionary order, so "apple" < "banana").

Comparisons can’t be chained: write a < b && b < c, not a < b < c.

OperatorMeaning
&&and: true if both sides are true
||or: true if either side is true
!not: flips true and false
let age = 30
let member = false
print(age >= 18 && !member)

The operands must be Bools. Tessel doesn’t treat 0 or "" as false.

These work on the bits of Ints (in two’s complement, as in most languages):

OperatorMeaningExampleResult
&and: bits set in both0b1100 & 0b10108 (0b1000)
|or: bits set in either0b1100 | 0b101014 (0b1110)
^exclusive or: bits set in one but not both0b1100 ^ 0b10106 (0b0110)
~xnot: every bit flipped~5-6
<<shift left1 << 101024
>>shift right, keeping the sign-16 >> 2-4
let (r, g, b) = (0x12, 0x34, 0x56)
let color = r << 16 | g << 8 | b
print(color) // 1193046
print(color >> 8 & 0xff) // 52
let bold = 1 << 0
let italic = 1 << 1
var style = bold | italic
style &= ~bold
print(style & italic != 0) // true
print(style & bold != 0) // false
  • Shifting by 64 or more gives 0 (or, for >> of a negative number, -1). Shifting by a negative amount stops the program with an error.
  • << doesn’t check for overflow: bits shifted past the top are lost, so 1 << 63 is the smallest Int.
  • On Bools, use &&, || and != instead: a & b on two Bools is an error that suggests &&.
OperatorSame as
x = vset x to v
x += vx = x + v (numbers, strings, lists and Data)
x -= vx = x - v
x *= vx = x * v
x /= vx = x / v
x %= vx = x % v (Int and Float)
x &= v, x |= v, x ^= vx = x & v, … (Int)
x <<= v, x >>= vx = x << v, … (Int)

Assignment is a statement, not an expression: you can’t write a = b = 0 or use x += 1 as a value. The left side can be a variable, a field (player.score += 1) or an item (scores[0] = 10).

a..b is the range of whole numbers from a up to, but not including, b. You use it with for:

for i in 0..3 {
print(i) // prints 0, 1, 2
}

If b is not greater than a, the range is empty and the loop body doesn’t run. There is no inclusive range operator; write 1..(n + 1) to include n.

A range is a value like any other, of type Range. You can store it in a variable, pass it to a function, compare two ranges with ==, and print it:

let lines = 2..5
for i in lines {
print(i) // prints 2, 3, 4
}
print(lines) // 2..5
print(lines == 2..5) // true

To repeat something without using the number, name the loop variable _: for _ in 0..3 { … }.

These are covered on their own pages:

  • ?? gives a default for a missing optional value, and ?. reads a member only if there is a value. See Optionals.
  • . reads a field or calls a method, () calls a function, and [] reads an item from a list or dictionary.

From lowest to highest. Operators on the same row are applied left to right, except ??, which groups to the right.

Operators
||
&&
== != < <= > >=
..
??
+ - | ^
* / % &
<< >>
! - ~ (prefix)
. ?. () []

The bit operators sit where Swift puts them: shifts bind tighter than *, & is like *, and | and ^ are like +. So 1 + 2 << 3 is 1 + (2 << 3), and flags & mask == 0 is (flags & mask) == 0. Use parentheses when you mix | or ^ with + or -, which group left to right with them: 1 | 2 + 4 is (1 | 2) + 4.

Because ?? is lower than +, count ?? 0 + 1 would mean count ?? (0 + 1), which is rarely what’s meant. So Tessel asks for parentheses when ?? is followed by arithmetic: write (count ?? 0) + 1, or count ?? (0 + 1) if that’s what you mean.

A statement ends at the end of its line. Tessel doesn’t use semicolons; a ; is an error. Put each statement on its own line.

A statement can still span several lines:

  • while a ( or [ is open,
  • after an operator such as +, && or =,
  • when the next line starts with ., which continues a chain of calls.
let total = 1 +
2 +
3
let names = [
"Ada",
"Grace",
]
let shout = names
.joined(separator: " and ")
.uppercase()
print(shout)

A trailing comma after the last item in a list, like "Grace", above, is allowed.

To split a long expression, end the line with the operator, as above. A line that starts with an operator doesn’t continue the line before it, because that line already ended its statement. A line starting with +, *, &&, ?? or another operator that needs a value on its left is an error. A line starting with - is a new statement that works out a negative value and throws it away, so Tessel reports that too:

let a = x * 2.0
- 1.0 // error: this line's value isn't used
let b = x * 2.0 -
1.0 // fine: the `-` at the end of the line continues it

Any line that only works out a value with operators, like a + b, -a, !done or n == 5, and does nothing with it is an error. The last line of a block that gives a value is that value, so -x there is fine.