Skip to content
Draft

Types

Draft is statically typed with type inference. This chapter covers all built-in types.

Signed 64-bit integer:

@count: Int = 42
@negative: Int = -100
@big: Int = 9_223_372_036_854_775_807

64-bit floating-point number:

@price: Float = 19.99
@pi: Float = 3.14159
@scientific: Float = 1.5e10

true or false:

@active: Boolean = true
@done: Boolean = false

UTF-8 encoded string:

@name: String = "Draft"
@empty: String = ""
@multiline: String = "Line 1
Line 2
Line 3"

Single Unicode character:

@letter: Char = 'A'
@digit: Char = '9'
@emoji: Char = ''

Represents absence of a value:

@result: String = nil

Ordered collection of same-type elements:

@numbers: Array<Int> = [1, 2, 3, 4, 5]
@names: Array<String> = ["Alice", "Bob", "Charlie"]
@empty: Array<Float> = []

Key-value pairs:

@scores: Map<String, Int> = {"Alice": 95, "Bob": 87}
@config: Map<String, String> = {"host": "localhost", "port": "8080"}

Unique elements:

@unique: Set<Int> = {1, 2, 3, 4, 5}
@letters: Set<Char> = {'a', 'b', 'c'}

Fixed-size, heterogeneous collection:

@pair: (String, Int) = ("Alice", 30)
@triple: (String, Int, Boolean) = ("Bob", 25, true)

Named fields with fixed structure:

record Point
x: Float
y: Float
fin record.
@origin = Point { x: 0.0, y: 0.0 }

Values that may or may not exist:

@maybe: Option<Int> = some(42)
@nothing: Option<Int> = none

Values that represent success or failure:

@success: Result<String, Error> = ok("data")
@failure: Result<String, Error> = err(FileNotFound)

Actions as first-class values:

@callback: (Int, Int) -> Int = fn(@a, @b) => @a + @b

Create custom names for types:

type UserId = Int
type UserName = String
type Callback = (String) -> Void

A value that can be one of several types:

type StringOrInt = String | Int
action process(@value: StringOrInt)
match @value
when String then write("String: " + @value)
when Int then write("Int: " + string(@value))
fin match.
fin action.

Explicit conversion between types:

@num = int("42") @// String to Int
@str = string(42) @// Int to String
@flt = float("3.14") @// String to Float
@strs = str(3.14) @// Float to String
@bool = bool(1) @// Int to Boolean

Check types at runtime:

if @value#is_type(Int)
write("It's an integer")
fin if.

A type that can hold nil:

@maybe: String? = nil
@definite: String? = "hello"
action check(@value: String?)
if @value != nil
write(@value)
fin if.
fin action.

Represents no return value:

action log(@message: String) -> Void
write(@message)
fin action.

A type that can hold any value (use sparingly):

@anything: Any = "can be anything"

Learn about Actions to define functions in Draft.