#Why do I not see a compiler error here?

1 messages · Page 1 of 1 (latest)

tropic crypt
#

Hi,

For zig 0.10.0, this compiles without an error. Where's the flaw in my mental model?

const Point = struct {
    x: u32,
    y: u32,
    
    // Parse a string '123,456' as a point.
    fn parse(s: []const u8) Point {
        // write string parser function    
        _ = s;
    }
};

pub fn main() !void {
}

Shouldn't the compiler complain that parse has not the correct return type or doesn't return anything at all, respectively?

feral mango
#

you aren't calling the function!

red garden
#

If Point.parse is never referenced, either directly or indirectly, from main, it is never analyzed! This is Zig's lazy analysis model and is crucial to making some things work. There is no good info in the reference manual about this yet, otherwise I would link you to a helpful section

feral mango
#

^

red garden
#

auguste and I are both so excited about it we both used exclamation marks

#

so you know it's a good feature

tropic crypt
#

😄 Thanks, I'm not used to a lazy analysis model, your explanation makes sense.

lusty bay
#

@tropic crypt there's this trick

test {
    @import("std").testing.refAllDeclsRecursive(@This());
}``` ~~and if you wanna run it outside tests too use `comptime` instead of `test`~~
tropic crypt
#

Thanks. Very helpful!