4 ms·
To be fair, a similar argument also applies to structs vs. tuples. Unpacking them is really awkward. I suspect language designers only tolerate them because t
by Jweb_Guru 12y ago
To be fair, a similar argument also applies to structs vs. tuples. Unpacking them is really awkward. I suspect language designers only tolerate them because they're so convenient in practice for representing mathematical tuples (where order is actually semantically significant) and have very lightweight syntax in cases where you want to use all or most of the values. But with variants, order is never meaningful and you can only have one disjunctive type at at time (which quashes both those points). There's a Rust RFC which proposes some rather icky syntax for them (match foo { (e|!|!) => ... | (!|i|!) => ... }) and that alone convinced me that this is a no-go.
- benjaminjackman 12y agoOrder is important in rust variants? That sucks, hmm I hadn't thought about it in this much depth and it's probably obvious from literature seems like there is a whole spectrum of variants then: 1. wrapper variants (where the choosen instance is given a key or it's own wrapper type) 2. ordered variants (where the choosen instance if keyed by it's position) 3. type variants (The only way to differentiate is by type, duplicate types collapse into one type) I obviously prefer type variants where: Int | Int | Int simplifies to Int I think it gives the typechecker the ability to make and understand a much larger variety of useful properties. For example a method that takes def print(x: Num | String) it will accept Int | String or String | Int or String | Double or String or Int or Double obviously that isn't totally ideal, you probably rather use polymorphism. (I think I read that the Ceylon implementors thought it would be a big win here then they preferred to use polymorphism, but I could be spreading FUD against my own postion :) BTW I would argue that type variants have the opposite property of tuples in that as they grow longer that make code much more comprehensible.
- comex 12y agoOrder isn't currently significant; it's the RFC (which I haven't seen) which proposed adding this in some form.
- benjaminjackman 12y agoWow, I just looked over some of them and I think it's awesome that Rust has these RFCs tied to pull requests. Scala has SIPs but they are much less frequently used, and their granularity is much more coarse, yet they are less detailed and commented on by the community.