~andrewrk/ziglang

Re: static/comptime? map

Details
Message ID
<DBAP190MB0982BA3FD4058D8F727CE17EF9779@DBAP190MB0982.EURP190.PROD.OUTLOOK.COM>
DKIM signature
pass
Download raw message
Hi Johnathan,  I tried using the map as a type, I think... :)

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

pub const stepHolder = struct {
     map: stepMap,
};

pub fn init() void {
     js.logU8("two==", stepMap.get("2") orelse 255); //works
     //var a = stepHolder{ .map = stepMap };             //error: 
expected type 'std.comptime_string_map.ComptimeStringMap(u8,(struct 
app-workflow-type-experiment.struct:19:44 constant))', found 'type'
     var b = stepHolder{ .map = undefined }; //assuming we have a new 
'type', and no instance required
     js.logU8("five==", b.map.get("5") orelse 255); //error: expected 1 
argument(s), found 2
}

Unfortunately no progress, assuming this is what you were meaning?  
Apologies if it formats badly.  Cheers, Peter



On 27/08/2022 10:35, Peter Bridge wrote:
> 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.
>>
>>
>> .
>>
>
Reply to thread Export thread (mbox)