New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failable functions returning nullable don't infer error sets or cast literals to nullables. #852

Closed
Hejsil opened this Issue Mar 21, 2018 · 0 comments

Comments

Projects
None yet
2 participants
@Hejsil
Member

Hejsil commented Mar 21, 2018

When returning null, error set is not inferred:

test "" {
    var v = strToValue("null");
}

// error: function with inferred error set must return at least one possible error
fn strToValue(str: []const u8) !?i64 {
    if (str[0] == 'n')
        return null;

    return error.T;
}

When returning literal, we get:

test "" {
    var v = strToValue("null");
}

// error: unable to make error union out of number literal
fn strToValue(str: []const u8) !?i64 {
    if (str[0] == 'n')
        return 1;

    return error.T;
}

If we cast the literal, it all works, and we can even return null properly:

test "" {
    var v = strToValue("null");
}

// Hurray! It works!
fn strToValue(str: []const u8) !?i64 {
    if (str[0] == 'n')
        return i64(1);
    if (str[0] == 'p')
        return null;

    return error.T;
}

@Hejsil Hejsil added the bug label Mar 21, 2018

@andrewrk andrewrk added this to the 0.3.0 milestone Mar 21, 2018

@Hejsil Hejsil closed this in 2fc34ea Apr 28, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment