You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Perhaps I'm missing something w.r.t. the semantics of what I'm doing or with the design of the compiler, but I did some playing with this issue and can't understand the asymmetry.
Context: I'm working on a lexer based on the design of the lexer in Rob Pike's talk "Lexical Scanning in Go". One of the keys to the lexer implementation is this function type definition. Naively translated to Zig it looks like this:
constStateFn=fn(lexer: *Lexer) StateFn;
That fails to compile because StateFn depends on itself, as expected. However, after some digging into recursive structs I find that structs can contain pointers to instances of themselves if annotated correctly.
constStateFn=fn(lexer: *Lexer) ?*constStateFn;
Still won't get accepted by the compiler. So I keep digging and eventually find I can define a struct as follows:
This will compile. Interestingly removing the optional specifier from the return type will cause the compiler to segfault but that seems to be related to another bug, just an interesting hiccup I noticed. In any case, inside the struct I can define functions to the tune of:
The comptime wrapping is to satisfy some of my paranoia about senseless allocations.
As I understand it, all that's happening in this code is the passing of function pointers, which is what I would be looking to do with the simpler code as well. Perhaps I'm missing something subtle in type checking or elsewhere. But it would be really nice to support the simpler form and I'm eager to submit a patch and imagine I could with a little guidance w.r.t. both the language and compiler design.
The text was updated successfully, but these errors were encountered:
Perhaps I'm missing something w.r.t. the semantics of what I'm doing or with the design of the compiler, but I did some playing with this issue and can't understand the asymmetry.
Context: I'm working on a lexer based on the design of the lexer in Rob Pike's talk "Lexical Scanning in Go". One of the keys to the lexer implementation is this function type definition. Naively translated to Zig it looks like this:
That fails to compile because StateFn depends on itself, as expected. However, after some digging into recursive structs I find that structs can contain pointers to instances of themselves if annotated correctly.
Still won't get accepted by the compiler. So I keep digging and eventually find I can define a struct as follows:
This will compile. Interestingly removing the optional specifier from the return type will cause the compiler to segfault but that seems to be related to another bug, just an interesting hiccup I noticed. In any case, inside the struct I can define functions to the tune of:
The
comptime
wrapping is to satisfy some of my paranoia about senseless allocations.As I understand it, all that's happening in this code is the passing of function pointers, which is what I would be looking to do with the simpler code as well. Perhaps I'm missing something subtle in type checking or elsewhere. But it would be really nice to support the simpler form and I'm eager to submit a patch and imagine I could with a little guidance w.r.t. both the language and compiler design.
The text was updated successfully, but these errors were encountered: