Skip to content

Functions

Andrew Johnson edited this page Nov 20, 2024 · 3 revisions

Functions and methods are an important part of programming in LSTS.

Function Declaration

Functions are declared with the let keyword.

let f(x: U64, y: U64): U64 = x + y;

print( f(123, 456) );

Method Declaration

Methods are declared with a dot . before their name.

let .f(self: MyObject, y: U64): U64 = self.x + y;

print( o.f(123) );

Field syntax is just a sugared method call.

a.f

and 

a.f()

are exactly equivalent to

$".f"(a)

however

(a.f)()

is different because it is equivalent to

$".f"(a)()

Function Expressions and Lambdas.

Function Expressions and Lambdas can be created with the fn keyword.

[1,2,3].map( fn f(x: U64): U64 = if x>10 then x*x else f(x-1) );

[1,2,3].map( fn(x: U64): U64 = x*x );
Clone this wiki locally