#112ordered structs (equivalent of `struct A(B)` in rust)

  • avatar~clovercommented · 2 years ago
    Author
    Owner

    right now tuples cast to each other, exactly like arrays do. this is how rust and zig both works. additionally, this behavior also makes @Im-Beast's proposal about allowing field names in tuples impossible, since you could have multiple definitions with different names.

    it seems we want to have an ordered struct that

    • is nominal
    • can access fields by name or index

    an example of such struct could be like Point/Vec3 where there are three components which are ordered, and you may want to have it be a distinct type instead of just "three numbers"

    syntax demonstration

    struct Vec2 (float, float)
    
    let a = Vec2(2.3, 6.2);
    let b: Vec2 = .(2.3, 6.2);
    assert a.0 == 2.3;
    assert b.1 == 6.2;
    
    // with textual field names
    
    struct Vec3 (x: float, y: float, z: float)
    let c: Vec3(1, 1, 1);
    assert c.y == c.1;
    

    then, tuples become almost exclusively for the existence of two values in a loose way, mostly in returning two values from a function. here is an example of a function signature from the compiler that uses this pattern (to be fair, this is in zig and not the language itself)

    fn transformAssignmentTarget(
        t: *Transform,
        ctx: BlockContext,
        node: NodeIndex,
    ) Allocator.Error!?struct { AssignmentTarget, ResultType } {
    

    i believe that Beast and @lino-levan thought that tuples were going to be what this proposal implements.


    Milestone: 0.3.0.

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