#?/! types and compiler errors

1 messages · Page 1 of 1 (latest)

somber reef
#

When using optionals/errors, I find myself accidentally forgetting to unwrap first, and I have to scan the compiler error a few times before I realize what the problem is. For example:

    const result = m.some_field + 3;

When running zig test, I get this error:

    _ = m.some_field + 3;
        ~^~~~~~~~~~~

That sentence is totally true. However, when you're scanning output (especially big console logs), its easy to miss the ? or the ! (single character at the front of the string, I'm usually looking at the end of the string to figure out which type it was that failed). Is there a way to get the compiler to check if the unwrapped ? thing supports field access and then print a message suggesting that that might be the problem? Or make it more obvious that this is an optional/error type? Something like:

type '?test.MyData' does not support field access, but 'test.MyData' does. Possibly need to unwrap the optional?

or even:

Optional type '?test.MyData' does not support field access.

hard hedge
#

afaik there's no flag or such to make it do that but improving error messages is something the compiler will want to do even past 1.0
this is a perfect PR candidate (tho i wouldn't know where to begin)

bold sparrow
#

Yea, this one tripped me up many times. Would be great to improve the error with a hint.

somber reef
#

Ok, sounds like an issue then

bold sparrow
#

when you forget a try, the error type is often super long too so those messages are a bit obtuse as well

somber reef
#

yeah great point