Types
Draft is statically typed with type inference. This chapter covers all built-in types.
Primitive Types
Section titled “Primitive Types”Signed 64-bit integer:
@count: Int = 42@negative: Int = -100@big: Int = 9_223_372_036_854_775_80764-bit floating-point number:
@price: Float = 19.99@pi: Float = 3.14159@scientific: Float = 1.5e10Boolean
Section titled “Boolean”true or false:
@active: Boolean = true@done: Boolean = falseString
Section titled “String”UTF-8 encoded string:
@name: String = "Draft"@empty: String = ""@multiline: String = "Line 1Line 2Line 3"Single Unicode character:
@letter: Char = 'A'@digit: Char = '9'@emoji: Char = ''Represents absence of a value:
@result: String = nilCollection Types
Section titled “Collection Types”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'}Composite Types
Section titled “Composite Types”Tuples
Section titled “Tuples”Fixed-size, heterogeneous collection:
@pair: (String, Int) = ("Alice", 30)@triple: (String, Int, Boolean) = ("Bob", 25, true)Records
Section titled “Records”Named fields with fixed structure:
record Point x: Float y: Floatfin record.
@origin = Point { x: 0.0, y: 0.0 }Optional Types
Section titled “Optional Types”Values that may or may not exist:
@maybe: Option<Int> = some(42)@nothing: Option<Int> = noneResult Types
Section titled “Result Types”Values that represent success or failure:
@success: Result<String, Error> = ok("data")@failure: Result<String, Error> = err(FileNotFound)Function Types
Section titled “Function Types”Actions as first-class values:
@callback: (Int, Int) -> Int = fn(@a, @b) => @a + @bType Aliases
Section titled “Type Aliases”Create custom names for types:
type UserId = Inttype UserName = Stringtype Callback = (String) -> VoidUnion Types
Section titled “Union Types”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.Type Conversion
Section titled “Type Conversion”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 BooleanType Checking
Section titled “Type Checking”Check types at runtime:
if @value#is_type(Int) write("It's an integer")fin if.Nullable Types
Section titled “Nullable Types”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.Void Type
Section titled “Void Type”Represents no return value:
action log(@message: String) -> Void write(@message)fin action.Any Type
Section titled “Any Type”A type that can hold any value (use sparingly):
@anything: Any = "can be anything"Next Steps
Section titled “Next Steps”Learn about Actions to define functions in Draft.
