LEP-003: Statements


Created:Thursday, May 5, 2022
Author:David Delassus
Category:Language Design
Status:FINAL

Abstract

This LEP specifies the statements available in Letlang and their syntax.

Rationale

Every expression[1] evaluates to a value, while statements[2] perform actions and do not return any value. Reducing the number of different kind of statements to a minimum gives more freedom to the developer, and increase their ability to compose computations together.

Letlang aims to provide only 6 statements, at the root of a module. Every other concept is provided as an expression to use within those statements.

Specification

Module statement

This is the first statement of every Letlang module, it defines its name:

module "foo.bar.baz";

Import statement

This statement establish a dependency relation with another Letlang module, and bring it into scope:

import "foo.bar.baz";
# or
import "foo.bar.baz" as "foo";

Constant definition

This statement creates a symbol associated to a single value:

const pi = 3.14;

This statement is either:

  • private: only the current module can use it
  • public: modules importing the current module can use it

To define a public constant, prefix the statement with the pub keyword:

pub const pi = 3.14;

Class definition

This statement creates a new Letlang type[3]:

class int(n: number) {
  frac(n) = 0;
}

class even(n: int) {
  n % 2 = 0;
}

This statement is either:

  • private: only the current module can use it
  • public: modules importing the current module can use it

To define a public class, prefix the statement with the pub keyword:

pub class int(n: number) {
  frac(n) = 0;
}

Function definition

This statement defines a new function[4]:

func add(a: number, b: number) -> number {
  a + b;
}

This statement is either:

  • private: only the current module can use it
  • public: modules importing the current module can use it

To define a public function, prefix the statement with the pub keyword:

pub func add(a: number, b: number) -> number {
  a + b;
}

Effect definition

This statement defines the signature for a side effect[5]:

effect log(msg: string) -> @ok;

This statement is either:

  • private: only the current module can use it
  • public: modules importing the current module can use it

To define a public effect, prefix the statement with the pub keyword:

pub effect log(msg: string) -> @ok;

Rejected Ideas

Export statement

The usage of the keyword pub has been prefered over the usage of an explicit export statement to reduce the number of lines of code in a module.

References

ReferenceTitleLink
1Expressionhttps://en.wikipedia.org/wiki/Expression_(computer_science)
2Statementhttps://en.wikipedia.org/wiki/Statement_(computer_science)
3LEP-004: Type System/lep/004/
4LEP-005: Functions/lep/005/
5LEP-006: Side Effects/lep/006/