Anonymous sum types
Nobody has claimed this yet.
- Dominant language
- Markdown
- Stars
- 6.6k
- Forks
- 1.7k
- Avg merge
- 16h 14m
- Merged PRs (30d)
- 1
Description
Issue by glaebhoerl
Saturday Aug 03, 2013 at 23:58 GMT
For earlier discussion, see https://github.com/rust-lang/rust/issues/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.)
SEE ALSO
- #402
- #514
- #1154
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the earlier discussion in #8277, then review related issues #402, #514, and #1154. Evaluate the proposed (~str|int|int) syntax and ! placeholders, including possible conflicts with closures and negation. Done would require a decided, demanded design rather than merely recording the idea.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 15/100