~andrewrk/ziglang

1

[_]struct within [_]struct (was comptime map)

Details
Message ID
<DBAP190MB09823C2AABBD43BA363E855FF9429@DBAP190MB0982.EURP190.PROD.OUTLOOK.COM>
DKIM signature
missing
Download raw message
My latest attempt with structs requires the array to be outside of the 
'steps' definition so I can keep the array handle, but pass over a 
slice...  ideally I would like the event mappings to be defined with the 
steps definition...

I just discovered Zig tuples, and thinking it might offer a cleaner 
solution.  But I am struggling to find documentation on them, ie how to 
use them and memory ownership.  Any tips please?


const STEP_START_EVENTS = [_]EventMapping{EventMapping{
     .event = EVENT_NEXT,
     .nextStep = STEP_SPLASH,
}};

const STEP_SPLASH_EVENTS = [_]EventMapping{EventMapping{
     .event = EVENT_PREV,
     .nextStep = STEP_START,
}};

const steps = [_]StepDescriptor{
     StepDescriptor{
         .step = STEP_START,
         .debug = 2,
         .eventMapping = STEP_START_EVENTS[0..],
     },
     StepDescriptor{
         .step = STEP_SPLASH,
         .debug = 5,
         .eventMapping = STEP_SPLASH_EVENTS[0..],
     },
};
Details
Message ID
<4e588362-2691-4bec-8472-81087ac90f1e@www.fastmail.com>
In-Reply-To
<DBAP190MB09823C2AABBD43BA363E855FF9429@DBAP190MB0982.EURP190.PROD.OUTLOOK.COM> (view parent)
DKIM signature
missing
Download raw message
Hey Peter!

I've followed your thread for the past few weeks, and while I certainly can appreciate your concrete questions, I'm not sure I understand what your overall goal is. (i.e., maybe we've got an XY-problem at hand, where you ask for something that's awkward to do in Zig but the problem you're having could be solved by something different, which would be easy to do in Zig)  It looks like you have a bunch of states that each have a slice of other states that they'd want to invoke somehow, from an event or something. Could you be a little more descriptive?

I hope I can offer some insight on tuples though, because it's actually really simple; a tuple is just an anonymous struct, in which the struct fields have the names `0`, `1`, ... 

For instance,

const std = @import("std");
​pub fn main() !void {
    const foo = .{ 10, -2, 99 };
    std.debug.print("{}\n", .{ foo.@"1" });
}

prints `-2`. (The `@"1"` syntax is used to avoid keywords, or the lexer in general; the field is named `1`, because it is the second element in the anonymous struct initializer (I'm not confident about the nomenclature here), but `1` is not a valid identifier so you need this funny syntax. It's just like `foo[1]` in Python, or `foo.0` in Rust, or whatever) 

~martin
Reply to thread Export thread (mbox)