Skip to content

Types in EK

Lindon Aliu edited this page Jan 29, 2024 · 1 revision

There are several kinds of types in EK:

  • Atoms: These are used to represent a value with no data. For example, true, false and void are atoms.
  • Integers: Unlike other languages, integers have no minimum or maximum value.
  • Primitives: Strings, Floats
  • Structs: These are values that contain multiple elements.

You can declare your own atoms and your own structures. You can also make type aliases for typing your code.

Atom definitions

Atoms are defined using the atom keyword, and then the name of the atom.

Note that atoms are both types and values. For example, true is both the type of the value true and the value true itself.

atom true
atom false

Type aliases

Type aliases are defined using the type keyword, and then the name of the type alias, an equals sign, and then the type that the alias refers to.

type bool = true | false

Structs

Structs are defined using the struct keyword, and then the name of the struct, and then the fields of the struct.

struct Point {
    x: int,
    y: int,
}

Note that the comma after the last field is optional.

Structs can be constructed using the struct constructor syntax:

Point { 1, 2 }

The fields are written in the order that they are defined in the struct definition, separated by commas.

Structs fields can be accessed using the field name as a postfix function:

fn p = Point { 1, 2 }
fn test : int = p x

Union types

Union types are multiple types that are combined into one type, using the | operator. For example, true | false is a union type that can be either true or false. As such, the standard library defines a type alias bool for this type.

Integer ranges

Integer ranges are defined using brackets and the .. operator. For example, [0..9] is a type that can be any integer from 0 to 9, inclusive.

type digit = [0..9]

Note that integer ranges are inclusive on both ends. The bounds are optional, and if they are omitted, then the range is unbounded on that end.

type positive = [1..]
type negative = [..-1]
type int = [..]

Also, ranges can be replaced with a union of integers. For example, [0..1] is the same as 0 | 1. It is however more convenient to use ranges, especially when the range is large.

Any integer literal can be used as a type. For example, 42 is a type that can only be the value 42. It is equivalent to [42..42].

String type

The string type is a primitive type. It can refer to any string value. String literals are of type string, and are written using double quotes.

fn example : string = "This is a string literal."

Float type

The float type is a primitive type. It can refer to any floating point value. Float literals are of type float, and are written using a decimal point.

fn example : float = 3.14

Function types

Function types are written using the -> operator. For example, int -> int is a function that takes an integer and returns an integer. Function types can be nested, and are right associative. For example, int -> int -> int is a function that takes an integer and returns a function that takes an integer and returns an integer.

fn add : int -> int -> int = _ + _

Clone this wiki locally