We’ve seen that Rye code can consist of literal values, for example:
33 ; integer
"Hello word" ; text
https://example.com ; URL
'word ; literal word
{ 1 2 3 } ; block of integers
{ "Hello" "Word" } ; block of texts
These values can be assigned to words using set-words, i.e. words that have a colon on their right.
age: 33
message: "Hello World"
website: https://example.com
type: 'word
numbers: { 1 2 3 }
words: { "Hello" "World" }
If you’ve programmed in any language before, these would be the constants. The default assignment in Rye is to a constant.
We haven’t looked at function calls yet, but the use of the print
function below will be self-evident I hope.
print "Hello world!"
; prints: Hello world!
message: "Hello Mars!"
; assigns text "Hello Mars!" to word message
print message
; prints: Hello Mars!
Values that get assigned to words can also come from expressions.
Again we haven’t looked at op-words yet, but the effect of + is probably understandable.
meaning: 24 + 18
print meaning
; prints: 42
Everything in Rye returns a value. Use of set-words also return the assigned value, so they could be used inline within an expression.
fruits: 100 + apples: 12 + 21
; we set:
; apples: 33
; fruits: 133
You will see why later, but Rye also has set-words that take a value from the left.
"Jim" :name
12 + 21 :apples + 100 :fruits
All of the above assigns (binds) a value to a word and makes that word a constant in the current context, meaning, we can’t later change it. This makes code more predictable.
Word can also become a variable in it’s context by using a function var and a lit-words (literary word). Such words can be modified using the modifying functions (ending with a !) or a mod-word.
; 5 minutes to midnight
var 'age 42
sleep ( 5 * 60 ) .seconds
; age: 43 ; Would produce an Error
age:: 43
sleep 365 .days ; you would wait a while :)
change! 44 'age ; returns true if changed
Mod words are a little harder to look at, but that is by design. The goal is that you modify values under words only when you really need to.
The same holds true for the left mod-words. ::age
.