sysl

Functions

An expression after =, or an indented block. Defaults, names at the call, and closures.

Two bodies

A function is a name, a parameter list, a return type, and a body. When the body is a single expression, it follows =, and the return type may be left off: the function returns whatever type the expression has, just as a val takes the type of its value:

add(a: int, b: int) = a + b
greeting(name: string) = "hello, " + name

print("3 + 4 =", add(3, 4))
print(greeting("sysl"))
3 + 4 = 7
hello, sysl

A function that calls itself writes its return type, since its result cannot be read off a body that needs the answer first.

A longer body is indented under the header, without the =. The last expression is the function’s result, so no return is needed; return exists for leaving early:

frame_kind(payload: int) -> string
    var total = payload + 18

    if total > 1518 then "jumbo"
    else "standard"

print(frame_kind(9000), frame_kind(46))
jumbo standard

An optional end marker closes a long declaration and names it, which helps when the start of the block is off the screen. A block body with no = and no return type returns nothing: its last line is run for its effect.

greet(name: string)
    print("hi", name)

greet("you")
hi you

A parameter may have a default. Defaults are filled from the right, so the number of arguments a call passes decides how many defaults it uses:

tag(text: string, open: string = "[", close: string = "]") = open + text + close

print(tag("info"), tag("warn", "<"), tag("error", "{", "}"))
[info] <warn] {error}

A default is an expression evaluated at each call, not a single value computed once and shared. This matters when a default allocates: every call gets its own object.

An argument may also be passed by name, which keeps a call with several literal arguments readable:

window(x: int = 0, y: int = 0, width: int = 80, height: int = 24) -> string
    s"${width}x$height at $x,$y"

print(window(width = 132, y = 10))
132x24 at 0,10

A positional argument may not follow a named one: once a call starts naming arguments, the rest must be named too.

A name may be declared more than once. The arguments of a call, their number and their types, decide which declaration it uses:

show(x: int) = s"int $x"
show(x: string) = s"str $x"

print(show(1))
print(show("a"))
int 1
str a

The return type does not take part. Two declarations that differ only in their result could not be told apart by any call, so the second is rejected where it is declared. The reference has the full rules, including what happens when a call matches two declarations.

Closures

A parameter that takes a function has an arrow type. A single parameter type needs no parentheses; zero or several need them:

apply(f: int -> int, x: int) = f(x)
combine(f: (int, int) -> int, a: int, b: int) = f(a, b)

print(apply(x -> x + 1, 5), combine((a, b) -> a * b, 6, 7))
6 42

A closure body can use the names around it, which is what distinguishes it from a plain function pointer. Using a name captures it; there is no capture list to write:

apply(f: int -> int, x: int) = f(x)

var factor = 10

print(apply(x -> x * factor, 5))
50

As in Kotlin, Swift or JavaScript, a closure that does not outlive the variables it captures shares them: a write in it reaches the variable.

each3(f: int -> unit)
    for i in 0..<3 do f(i)

var total = 0

each3(i -> total += i * i)
print("the total is", total)
the total is 5

One rule differs from what you may expect. A closure that may outlive them — returned, stored in module storage or a buffer, kept by the function it is handed to — copies them: it has its own copy, and writing to it does not change the original, so a write that only the copy would see is refused rather than silently lost:

var later: Option[&Fn(int) -> unit] = None

keep(f: &Fn(int) -> unit) = later = Some(f)

var total = 0

keep(i -> total += i * i)
print("the total is", total)
this closure captures by value, since it is handed to 'keep' on line 7, which keeps its parameter 'f' (it is stored in 'later'), so it holds a copy of 'total' — so '+=' would reach that copy rather than the variable it came from, and nothing in the closure reads it back

The compiler works out which one a closure is from the bodies, and the refusal says what keeps it; nothing is written for either. A closure that copies and reads back what it wrote keeps its copy as state of its own between calls, and that is allowed.

To accumulate across calls in a closure that is kept, capture something that is shared. A &T is a counted reference, so capturing one retains the reference instead of copying the object, and every holder sees the same object:

import sysl.buf.{Buf, buf}

each3(f: int -> unit)
    for i in 0..<3 do f(i)

var seen: &Buf[int] = buf()

each3(i -> seen.push(i * i))
print("collected:", seen.len())
collected: 3

Which behaviour you get depends on the type of the captured variable, not on anything written where it is captured. It is the same question the memory chapter answers, and that chapter explains this rule in full.

The same question decides where a closure lives. A closure that does not outlive its frame holds its variables by reference and is built in that frame, so handing it to each allocates nothing. One that escapes, by being stored in a field or returned, holds copies and is placed in a counted heap box. The compiler makes this choice for you.


Next: structs and methods, which give a type data and behaviour.

Search

Esc
↑↓ to navigate ↵ to open Esc to close