logoalt Hacker News

Natfantoday at 4:23 PM1 replyview on HN

if firefly has no nulls, how do you indicate that a value is unset?


Replies

danudeytoday at 5:34 PM

In Firefly (as in Rust) you can define fields as Optional, so you can do Option[String]; that lets you say "this variable is a String but it might not be here". That lets you then check to see if something is set, rather than checking to see if it's null.

In Rust an Option is a separate thing that you need to disambiguate to use. For example:

    match result {
        // The division was valid
        Some(x) => println!("Result: {x}"),
        // The division was invalid
        None    => println!("Cannot divide by 0"),
    }
Likewise in Rust, you can't have a null pointer, but you can have an Optional pointer, which is either a pointer to something or is not anything.

Firefly seems to have a similar case structure, though the first example I could find is in the Exceptions section: https://www.firefly-lang.org/reference/exceptions

    grabOption[T](option: Option[T]): T {
        | Some(v) => v
        | None => throw(GrabException())
    }
show 1 reply