ok so it seems like 'type' is not the generics I suspected, and in this
case is returning a anonymous struct? and somehow the compiler knows
what methods are implemented... So this looks like the same problem I
mentioned earlier, ie it cant return something like a 'ReadOnlyMap'
interface 'wrapping' the struct, but instead can only return the anon.
struct. So there probably isnt a type I can define within my struct to
hold the reference? So I probably cant use this, since next I need a
nested static map.
But it makes me wonder if Go style interfaces would be worth considering
for Zig. I cant believe I am saying that, since I initially hated the
implicit vs explicit design. But currently the Zig interface
'workaround pattern' isnt that intuitive with the 'fieldParentPtr'
complexity either... and the complexity in the above comptime map
design, just a thought!
Anyway, back to the challenge of porting this bit of code, it seems like
the only current solution would be an array containing an array. And
the 'map keys' become this array index.It a shame though that to use a
(numeric) enum in that case still requires a heavy dusting of
'enumToInt'. Maybe something a future compiler enhancement could detect?
Cheers, Peter
On 24/08/2022 21:41, Peter Bridge wrote:
> update, same problem, a different part of my code, but I discovered '
> ComptimeStringMap' ! :) and thinking I can maybe use that...
>
> My question is what 'type' does this create? I managed to pass the
> created var into a function by using anytype...(already not ideal).
> But now I need to store it in a static struct.
>
> From the source code it almost looks like this is a way of annotating
> extra methods onto a comptime type? ie it adds a 'has' and 'get' onto
> my StepType?. But If try using StepType, I just get an error:
>
> expected type 'api.workflow.StepType', found 'type'
>
> Sorry for newbie questions when I know you are busy on the self
> hosting compiler stuff. Congrats on that btw :)
>
> Main:
>
> const stepStart = Workflow.StepType{
> .id = 0,
> .name = "STEP_START",
> };
> const stepMenu = Workflow.StepType{
> .id = 1,
> .name = "STEP_MENU",
> };
> const steps = std.ComptimeStringMap(Workflow.StepType, .{
> .{ stepStart.name, stepStart },
> .{ stepMenu.name, stepMenu },
> });
>
> //@compileLog("type:", @TypeOf(steps)); //this just showed
> 'type' and an error
> Workflow.init(steps, stepStart);
>
>
> Workflow:
>
> const State = struct {
> var currStep: StepType = undefined;
> var steps: >whatTypeIsThis< = undefined; //<- this is the crux
> };
>
> pub fn init(comptime steps: anytype, firstStep: StepType) void {
> State.steps = steps;
> State.currStep = firstStep;
> }
>
>
>
> On 24/08/2022 17:18, Peter Bridge wrote:
>> I am back on my project to port my Go/Wasm project to Zig/Wasm and
>> struggling with this bit of Go code for a couple reasons:
>>
>> type SceneType int
>>
>> var scenes = map[SceneType]webgl.Scene{
>> SCENE_MAIN: scenemain.CreateScene(app),
>> SCENE_ART: artscene.CreateScene(app, tileConfigurator),
>> ...
>> }
>>
>> 'Scene' being an interface.
>>
>> 1. I cant work out a way to have a type safe int in Zig.
>> 2. Does Zig plan to add support for similar compile time 'map' types?
>> 3. With Zig current interface design, I find that I need to store 2
>> things, the parent struct to keep it alive, and then the interface
>> returned by one of the struct functions. Is that correct?
>>
>> So I am thinking for (1) I could create a struct containing u8, since
>> an enum wouldnt give me type safety for my public API...
>>
>> For (2) I thought about using a slice/array, with the u8 from my
>> struct as the index. But using a static initialiser means the items
>> have to be in the correct order (vs map lookups)
>> scenes: [@enumToInt(SceneType.ENUM_COUNT)]SceneType
>>
>> For (3) I have no idea how to solve. I guess would need a var for
>> every parent type.
>>
>> Cheers, Peter/WhiteHexagon (Zig 0.8.1)
>