Rust is bad in the description, solutions must be explained to the description


struct Shape { id: u32, color: Color, kind: ShapeKind }
enum ShapeKind { Circle { r: f64 }, Rect { w: f64, h: f64 } }
match shape.kind { ShapeKind::Circle {..} ... }

Brother, it makes you write a lot of lines to reach one thing, and this is what I seek to finish. Imagine that you can do this.

envelope Shape {
    id: u32,                   
    color: Color = Color::Black,
    | Circle { r: f64 }            
    | Rect { w: f64, h: f64 }
    | Empty
}

Shared fields are connected directly without match.

Use structstruck - Rust

11 Likes

This feels like ML syntax. If we wanted it to match Rust's aesthetics, it should look more like this:

struct Shape {
    id: u32,                   
    color: Color = Color::Black,
    kind: enum {
        Circle { r: f64 },
        Rect { w: f64, h: f64 },
        Empty,
    },
}

This also requires a way to name these types. https://github.com/rust-lang/rfcs/pull/3444 could do the trick:

Shape {
    id: 1,
    kind: .Circle { r: 100.0 }, // or _::Circle { r: 100.0 }, if we have a shred of sense
}

But there should also be a way to directly name them - e.g.:

Shape {
    id: 2,
    kind: Shape::kind::Rect { w: 200.0, h: 300.0 },
}
2 Likes