4 Values and Variables
A program works on values: numbers, pieces of text, true and false, lists of things. This chapter is about the values Zeno has, and about the names you give them.
4.1 The kinds of value
Every value has a type, which says what kind of value it is and what can be done with it. These are the types every program meets first:
| Type | What it holds | Written like |
|---|---|---|
int |
a whole number | 42, -7,
0xFF, 1_000_000 |
float |
a number with a fraction | 3.14, -0.5,
2.5e-7 |
bool |
true or false | true, false |
str |
text | "Ada" |
list[int] |
several values, in order | [1, 2, 3] |
dict[str, int] |
values filed under keys | ["Ada": 36] |
none |
no value | none |
print(42, 3.14, true, "Ada", [1, 2, 3], ["Ada": 36], none)42 3.14 true Ada [1, 2, 3] ["Ada": 36] none
Later chapters add more: numbers with a unit (500 ms),
numbers with a size (u8), objects of your own classes, enum
values, and functions.
4.2 let: a name for a value
let gives a value a name:
let name = "Ada"
let born = 1815
print(name, "was born in", born)Ada was born in 1815
A name given with let keeps its value for as long as it
exists. Trying to give it another is a mistake, and the message names
the word that would allow it:
let count = 0
count = 1error: `count` cannot be changed. Declare it with `var` to allow that
--> main.zeno:2:1
|
2 | count = 1
| ^
1 error found. Nothing was run.
4.3 var: a name whose value changes
When the value is meant to change, declare the name with
var. After that, plain = gives it a new
value:
var count = 0
count = count + 1
count = count + 1
print(count)2
There is a shorter way to write count = count + 1:
count += 1. Every operator has a form like it, and Numbers and Operators lists
them.
A name is declared once, and only declared names can be given a value. So a misspelt name is a mistake, and never a new variable by accident:
var count = 0
cuont = 1error: `cuont` is not declared. Create it with `let` or `var` first
--> main.zeno:2:1
|
2 | cuont = 1
| ^
1 error found. Nothing was run.
In Python, x = 1 both makes a new variable and changes
an old one, so a typing slip quietly makes a second variable and the
first one never changes. Separating the two turns the slip into an
error. let is the short, ordinary word because most values
never change once they are made, and a program whose values do not
change is easier to follow. (Decision 1.6.)
4.4 const: a value fixed for the whole program
const LIMIT = 100
const GREETING = "hello"
print(LIMIT, GREETING)100 hello
A const says that the value belongs to the program, not
to a moment while it runs: a size, a limit, an address, a name. By
custom its name is written in capitals. Like a let, it can
never be given another value.
Unlike a let, its value must be known before the program
runs: numbers, text, other constants, the operators between them, and
lists and dicts of those. Something only the running program can work
out belongs in a let:
const NAME = input()error: this is only known when the program runs, and a `const` is fixed when the program is built. A `const` is written from numbers, text, other `const`s and operators. For a value that is worked out once while the program runs, use `let`
--> main.zeno:1:14
|
1 | const NAME = input()
| ^
1 error found. Nothing was run.
4.5 Types are optional
You can write the type of a name after a :, or leave it
out:
let port: int = 8080
let host = "localhost" # a str: Zeno sees that
var ratio: float = 0.5
print(host, port, ratio)localhost 8080 0.5
Where the type is left out, Zeno works it out from the value. Where it is written, the value must fit it, and from then on every value given to the name must fit it too:
var level: int = 3
level = "high"error: the new value should be int, but this is str
--> main.zeno:2:9
|
2 | level = "high"
| ^
1 error found. Nothing was run.
A whole number is not quietly made a float, even where a float is wanted. Write the decimal point:
let ratio: float = 1error: `ratio` should be float, but this is int. Write a number with a decimal point, as in `1.0`, and convert a value with `float( )`
--> main.zeno:1:20
|
1 | let ratio: float = 1
| ^
1 error found. Nothing was run.
Turning an int into a float where one is
stored would leave a value whose written type and real type disagree,
and in a compiled program it hides an instruction. Arithmetic is
different: 1 + 0.5 makes a new value, and nothing is
disguised. (Decisions 2.3 and 2.3b.)
A name must have a value from the moment it is declared. There is no
var x with nothing after it:
var total: interror: expected `=`, found the end of the line
--> main.zeno:1:15
|
1 | var total: int
| ^
Types, Written and Worked Out goes into all of this in depth: what Zeno works out, what it cannot, and the strict mode in which every type must be known.
4.6 Names
A name is made of letters, digits and _, and does not
start with a digit. Letters of any alphabet are allowed. Case counts:
total and Total are two names. By custom,
variables and functions are written like_this, classes and
enums LikeThis, and constants LIKE_THIS.
The words of the language, such as let, if
and take, can never be names, and the message says where
the word goes instead:
let take = 3error: expected a name for the variable, found `take`. `take` is a word of the language, so it cannot be a name. It goes in front of the type of a parameter: `frame: take Frame`
--> main.zeno:1:5
|
1 | let take = 3
| ^
The names of types (int, str,
list) and of the built-in functions (print,
len) are not reserved. They are names like any other, and a
program may declare its own. That is rarely a good idea, since the
built-in one is then out of reach in that block.
4.7 Blocks and scope
A pair of braces { } makes a block, and a name
declared in a block exists from its declaration to the end of that
block. This stretch of the program is the name’s scope:
let x = 1
if true {
let x = 2 # a new x, for this block only
let y = 3
print(x, y)
}
print(x) # the first x again2 3
1
After the block, y is gone:
if true {
let y = 3
}
print(y)error: `y` is not defined
--> main.zeno:4:7
|
4 | print(y)
| ^
1 error found. Nothing was run.
An inner block may declare a name that an outer block already has, and inside the inner block the name means the inner one. Two declarations of one name in the same block are a mistake:
let speed = 10
let speed = 20error: `speed` is already declared in this block
--> main.zeno:2:1
|
2 | let speed = 20
| ^
1 error found. Nothing was run.
A variable that lives exactly as long as its block has a lifetime you
can see in the layout of the code. In Python a variable made inside an
if goes on existing after it. (Decisions 3.1 and 3.2.)
4.8 Two names for one list
A name gives a value a name; it does not make a copy. When the value is a list or an object, two names can stand for the same one, and a change made through either is seen through both:
let a = [1, 2]
let b = a
b.append(3)
print(a)[1, 2, 3]
This also means that let fixes the name, not
what is inside the value. a above can never be given
another list, but the list it names can still grow. Numbers, text and
true or false cannot be changed inside, so for them the difference never
shows. Lists has more on this.
Every value has a type. let names a value that stays,
var one that changes, and const one fixed for
the whole program. Plain = only changes a name that already
exists. Types can be written after : or left for Zeno to
work out. A name lives until the end of the block it was declared
in.
Declare
var price = 10and then give it10.5. What does Zeno say, and how would you declare it so that both are allowed?Write
let x: str = 5andlet y: int = "5". Read both messages.Use
printas a variable name withlet print = 3, and then try to print something below it. What happened?Make two names for one list, change it through one, and print it through the other. Then do the same with two numbers and
var. Why is the result different?