#118Structs with defined representation

  • avatar~clovercommented · 2 years ago
    Author
    Owner

    This is motivated by #61's requirement to have code that reads in JSON. Right now all structures do not have defined representation, making it impossible to use JSON.parse

    I propose adding a representation mode:

    struct S repr(.json) {
       field: int,
       other: float,
    }
    
    // alternate
    struct(.json) S {
    
    //
    struct S(.json) {
    

    The repr clause takes a comptime enum (#117) value saying what representation shall be used.

    enum Representation {
      auto,
      json,
    }
    

    instead of following what zig has with extern struct, i think a better plan is to make this an enum, and let us explore more representation options. perhaps there can be one that represents it as a JS object with camel cased keys, since ix field names in my head are snake case.

    Prior art on using a comptime enum like this can be seen in zig with callconv, align, and addrspace.


    Milestone: 0.2.0.

  • avatar~clover changed the status to Todo · 2 years ago
  • avatar~lino-levancommented · 2 years ago

    Rust-y syntax for this:

    #[repr(json)]
    struct S {
       field: int,
       other: float,
    }
    
  • avatar~clovercommented · 1 year ago
    Author
    Owner

    I want to see if i can avoid making # do anything for now. It might be useful for an operator later.

    Maybe a better name for this is layout. While i think representation is more accurate to what it means, layout is shorter.