Tutorial
Welcome to the Skip programming language!
We are going to present you the language through a series of short exercises. Follow the instructions in the comments and click "Run" whenever you are ready. When you see a message that says "Pass", you are done. If you see an error: try again!
The function 'debug' lets you print any value. Feel free to use it whenever you need!
Skip is an expression based language, so the last value of a sequence is the one returned by default. Let's try it!
Expression based means that 'everything is an expression'. In that spirit if-then-else is an expression too!
Locals can be modified, but you need to use an exclamation mark (e.g. !x = 1
sets the value of x
to 1
).
Skip objects are immutable by default: Point(0, 0)
creates a new immutable object of type Point.
When an object starts to have too many fields, you should prefer named arguments.
Point {x => 0, y => 0}
creates a new Point with named arguments.
this with {x => 0}
creates a copy this
with the field x
set to zero.
Let's try the same exercise again, but this time try to use the with
construction.
Using with
is fine, but we can do even better!
A very common pattern when manipulating immutable object is to define the same local with a new version of the object: !this = this with {field => value}
.
It works, but it's a bit verbose, to remedy that Skip lets you write: !this.field = value
.
Let's try to write the same exercise one last time, but this time by using an '!'.
Now, let's write the mutable version. Notice the keywords 'mutable' placed in front of the field and the method.
this.!field = value
modifies object in place, much like in any other programming language.
Mutable objects must be explicitly created with the 'mutable' keyword.
For example, mutable Point{...}
creates a new mutable Point.
Both mutable and immutable methods are accessible until the object is frozen.
The freeze keyword turns any mutable object into an immutable copy
(note that the mutable methods won't be available anymore after freezing).
Creating a mutable object and then freezing it is an encouraged pattern in Skip, especially when dealing with collections.
The two most common type of collections are Vector
and Map
.
A Vector
is an array of values that is contiguous in memory and that can grow in its mutable form.
A Map
is a hashtable that can also grow in its mutable form.
Now let's get to the most interesting part: memoization! Memoization consists in remembering the results produced by a function. But lets try it with an example: run the code, then uncomment the keyword 'memoized' and run it again. You will see that the 'move' function is only called once on the second run.
Demo
A demo application is available at apps/bundler/. The readme contains instructions for running the app.