I'm not really a zig user yet, so correct me if i'm wrong, but isn't
this
> > const steps = std.ComptimeStringMap(Workflow.StepType, .{
> > .{ stepStart.name, stepStart },
> > .{ stepMenu.name, stepMenu },
> > });
creating the new type, and in this case you named the new type 'steps'?
That's why the '@TypeOf(steps)' just shows you 'type', as 'steps' _is_ a
type, and hence doesn't _have_ a type.
I would suggest just calling this new type 'StepMap' or something
(instead of 'steps').
Then this crux
> > Workflow:
> >
> > const State = struct {
> > var currStep: StepType = undefined;
> > var steps: >whatTypeIsThis< = undefined; //<- this is the crux
> > };
will just become 'var steps: StepMap = undefined;'
I have not tried this, but that's how I understand it should work.
Ah! interesting, and that would explain why I can pass the value as a fn
param with anytype! But since this is across a 'public' API I probably
cant use this approach then, since it means the user defined 'type' has
to be known on the API implementation side, vs something generic like a
map or interface. I will give it a try anyway, since I am curious about
being able to create custom types like this, and might be a way to
create custom 'string' type :)
I started yesterday with the array/slice approach, but the nested events
'map' is going to end up being a sparsely populated array, which seems ugly:
pub const MAX_EVENTS = 5;
pub const eventNext = Workflow.EventType{
.id = 0,
.name = "EVENT_NEXT",
};
pub const stepStart = Workflow.StepType{
.id = 0,
.name = "STEP_START",
.eventMapping = &init: {
var v: [MAX_EVENTS]Workflow.StepType = undefined;
v[eventNext.id] = stepMenu;
break :init v;
},
};
const steps = init: {
var v: [MAX_STEPS]Workflow.StepType = undefined;
v[stepStart.id] = stepStart;
v[stepMenu.id] = stepMenu;
break :init v;
};
My other thought this morning was to use json. The example on the main
Zig site makes it look simple and comptime based! :) I already found
that I can avoid the funny multi-line string syntax with an import, but
not found any examples of how I could handle my array of steps.
Although starting to suspect allocators might be involved :(
Anyway thank you for taking the time to look my code, and your helpful
response! Peter
On 26/08/2022 13:43, Jonathan Halmen wrote:
> I'm not really a zig user yet, so correct me if i'm wrong, but isn't
> this
>>> const steps = std.ComptimeStringMap(Workflow.StepType, .{
>>> .{ stepStart.name, stepStart },
>>> .{ stepMenu.name, stepMenu },
>>> });
> creating the new type, and in this case you named the new type 'steps'?
> That's why the '@TypeOf(steps)' just shows you 'type', as 'steps' _is_ a
> type, and hence doesn't _have_ a type.
> I would suggest just calling this new type 'StepMap' or something
> (instead of 'steps').
> Then this crux
>>> Workflow:
>>>
>>> const State = struct {
>>> var currStep: StepType = undefined;
>>> var steps: >whatTypeIsThis< = undefined; //<- this is the crux
>>> };
> will just become 'var steps: StepMap = undefined;'
>
> I have not tried this, but that's how I understand it should work.
>
>
> .
>