IssueRaiser

Anonymous sum types

<a href="https://github.com/glaebhoerl"><img src="https://avatars.githubusercontent.com/u/1216629?v=2" align="left" width="96" height="96" hspace="10"></img></a> **Issue by [glaebhoerl](https://github.com/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 some

Posted by rust-highfive on Sep 24, 2014, 4:59 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.9,000 issues0 comments

github (Software)

Anonymous sum types

rust-highfiveSep 24, 2014, 4:59 AM

<a href="https://github.com/glaebhoerl"><img src="https://avatars.githubusercontent.com/u/1216629?v=2" align="left" width="96" height="96" hspace="10"></img></a> **Issue by [glaebhoerl](https://github.com/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 some

Comments (0)

No comments yet. Start the discussion.

Comment on this issue