~andrewrk/ziglang

1

Re: static/comptime? map

Details
Message ID
<CMHZY9MZMMPI.20MVCC4QT5TAT@len>
DKIM signature
missing
Download raw message
Hi Peter,

> Unfortunately no progress, assuming this is what you were meaning?
yes, that is basically what i meant. unfortunately i did not look into
how the ComptimeStringMap actually works before my previous reply, it
was based solely on the code you provided and my understanding of the
type system.

The ComptimeStringMap is never supposed to be instantiated (i guess the
name kinda suggests that), hence what i suggested won't work for you.

I quickly tried to get your code running -- yet again without putting
too much thought into it, hope this helps.

const stepMap = std.ComptimeStringMap(u8, .{
     .{ "2", 2 },
     .{ "5", 5 },
});

pub const stepHolder = struct {
     map: type, // << this may have been what you had been looking for
};

pub fn init() void {
    // don't know what the 'js' was, so i replaced your logs with prints
     std.debug.print("two=={}", .{stepMap.get("2") orelse 255}); //works
     const a = stepHolder{ .map = stepMap }; // now has to be const
                                            // or comptime var
     std.debug.print("three=={}", .{a.map.get("5") orelse 255});
     const b = stepHolder{ .map = undefined }; // this also works
     // std.debug.print("five=={}", .{b.map.get("5") orelse 255}); //
     // << error: use of undefined value here causes undefined behavior

     _ = b;
}

cheers

Re: static/comptime? map

Details
Message ID
<DBAP190MB0982C7B534D9A314419D8838F9769@DBAP190MB0982.EURP190.PROD.OUTLOOK.COM>
In-Reply-To
<CMHZY9MZMMPI.20MVCC4QT5TAT@len> (view parent)
DKIM signature
missing
Download raw message
Hi Jonathan, Thanks!  So now I know the missing crux type, was actually 
'type', and the fn param can change from 'anytype' to 'type'.  (I also 
now know that I can instantiate a struct containing this type, as long 
as it is const or comptime var.)

But when I go back to the original 'crux' line of code, first it 
complains it has to be constant because of 'type', but const cant be 
re-assigned...  So now the problem is probably outside of Zig language 
support, since this suddenly feels like a missing constructor, or 
attempt at lazy/lateinit of a comptime/const.

Only alternative I can think, is to rewrite the comptime map to return 
an 'interface', but as you can see, my Zig might not quite be at that 
level yet ;)  so lets abandon this quest for now, and if I find a better 
solution I post back.  Many thanks for your time.  Peter


//API Server Impl
pub const State = struct {
     var steps: type = undefined; //<-crux
};

pub fn apiInit(comptime clientMap: type) void { //and this switched from 
'anytype' to 'type'
     js.logU8("two==", clientMap.get("2") orelse 255);
     State.steps = clientMap;
     js.logU8("five==", State.steps.get("5") orelse 255);
}

//API client side
const stepMap = @import("utils/comp-map-inf.zig").ComptimeStringMap(u8, .{
     .{ "2", 2 },
     .{ "5", 5 },
});

pub fn client() void {
     apiInit(stepMap);
}
Reply to thread Export thread (mbox)