Description
Issue by glaebhoerl
Saturday Aug 03, 2013 at 23:58 GMT
For earlier discussion, see rust-lang/rust#8277
This issue was labelled with: B-RFC in the Rust repository
Rust has an anonymous form of product types (structs), namely tuples, but not sum types (enums). One reason is that it's not obvious what syntax they could use, especially their variants. The first variant of an anonymous sum type with three variants needs to be syntactically distinct not just from the second and third variant of the same type, but also from the first variant of all other anonymous sum types with different numbers of variants.
Here's an idea I think is decent:
A type would look like this: (~str|int|int)
. In other words, very similar to a tuple, but with pipes instead of commas (signifying or instead of and).
A value would have the same shape (also like tuples), with a value of appropriate type in one of the "slots" and nothing in the rest:
let foo: (~str|int|int) = (!|!|666);
match foo {
(s|!|!) => println(fmt!("string in first: %?", s)),
(!|n|!) => println(fmt!("int in second: %?", n)),
(!|!|m) => println(fmt!("int in third: %?", m))
}
(Nothing is a bikeshed, other possible colors for it include whitespace, .
, and -
. _
means something is there we're just not giving it a name, so it's not suitable for "nothing is there". !
has nothing-connotations from the negation operator and the return type of functions that don't.)
I'm not sure whether this conflicts syntax-wise with closures and/or negation.
Another necessary condition for this should be demand for it. This ticket is to keep a record of the idea, in case someone else has demand but not syntax. (If the Bikesheds section of the wiki is a better place, I'm happy to move it.)