Skip to content
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

confusing compile error when returning number literal #2763

Closed
ghost opened this issue Jun 27, 2019 · 2 comments
Closed

confusing compile error when returning number literal #2763

ghost opened this issue Jun 27, 2019 · 2 comments
Milestone

Comments

@ghost
Copy link

ghost commented Jun 27, 2019

Apologies if this is a duplicate.

fn blah(n: i32) ?i32 {
    if (n == 0) {
        return null;
    } else if (n < 0) {
        return -1;
    } else {
        return 1;
    }
}

test "" {
    _ = blah(0);
}

Outputs:

/home/dbandstra/oxid/test.zig:1:17: error: unable to make maybe out of number literal
fn blah(n: i32) ?i32 {
                ^

The solution is to return i32(1) but it's not clear from the error.

Also, I think these are usually referred to as "optionals", not "maybes", so the wording could be changed.

@andrewrk andrewrk added this to the 0.5.0 milestone Jun 27, 2019
@andrewrk
Copy link
Member

Related: #2749

@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Sep 20, 2019
@thejoshwolfe
Copy link
Sponsor Contributor

thejoshwolfe commented Dec 7, 2019

The solution is to return i32(1) but it's not clear from the error.

I would call that a workaround, not a solution. The compiler's behavior here makes no sense, since it's obvious which sequence of implicit casts should happen to make it all work.

I just tripped over this issue here: https://github.com/thejoshwolfe/legend-of-swarkland/blob/b60a915d3301737f0a49ce70eec024817b90d45c/src/server/game_engine.zig#L1045-L1075

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants