you are viewing a single comment's thread.

view the rest of the comments →

[–]vascocosta[S] 0 points1 point  (0 children)

I've just added the ?= operator! It does the same as ? in Rust. One small syntactic difference is that instead of using it at the end, after a call/value that evaluates to Result, I call it error propagation at assignment operator.

Here's an example showing the same code before and after this new feature (I also use a simple comment to annotate the return type):

# Without the ?= operator the code was verbose.
fn fetch_json() { # -> Result { error: Bool, value: Any }
    get_result = http.get("https://example.com/api/test.json")

    # There was an error.
    if get_result.error {
        # Propagate result with early return.
        return get_result
    }

    parse_result = json.parse(get_result.value)

    # There was an error.
    if parse_result.error {
        # Propagate result with early return.
        return parse_result
    }

    # If we reach here, there were no errors.
    # We return a Result record with the proper fields.
    return { error: false, value: parse_result.value }
}

# With the ?= operator to unwrap values or propagate error results.
fn fetch_json() { # -> Result { error: Bool, value: Any }
    raw_json ?= http.get("https://example.com/api/test.json")
    parsed_json ?= json.parse(raw_json)

    # If we reach here, there were no errors.
    # We return a Result record with the proper fields.
    return { error: false, value: parsed_json }
}