Skip to content

Optional Types #120

@mrodz

Description

@mrodz

Syntax

maybe_an_int: int? = some_function_call()
maybe_give_int = fn() -> int? {
    if math.rand() * 2 == 1 {
        return 42
    }

    return nil
}
# Initialize later

coolest_person: str? = nil

# Do some work...

print coolest_person # Some(Person { name: "Mateo", age: 16 })

?= and get

Will resolve as true if the variable exists, and False if it does not exist.

maybe_best_fruit: str? = read_fruit_db()

if best_fruit ?= maybe_best_fruit {
    print best_fruit
} else {
    print "maybe_best_fruit is empty"
}

best_fruit = get maybe_best_fruit : "Guava"

// has the potential to abort if maybe_best_fruit is nil
best_fruit_dangerous = get maybe_best_fruit

print best_fruit
print maybe_best_fruit

none_str: str? = nil

Metadata

Metadata

Assignees

Labels

bytecodeIssues related to bytecodecompilerIssues related to compilation & parsingenhancementNew feature or request

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions