IssueRaiser

Sum/union types

[Edit by eernstg: I removed 'type matching' from the title because Dart has pattern matching today.] I'd like to import some features from functional languages like OCaML and F#. The main feature I'm proposing is the possibility of using `typedef` to define sum types. They can be thought of as type enumerations. This will, in some cases, reduce the need for `dynamic` making for sounder typing. For example `json.decode` would no longer return `dynamic` but JSON a sum type, something like: ``` typedef JSON is Map<String, JSON> | List<JSON> | Literal; ``` Sum types could power a new behaviour of switch case that resolves the type based on the typed enumerated. Kind of like `assert` and `if` does with contemporary `is` syntax. ``` typedef T is A | B | C; ... void foo(T arg) { switch(arg) { case A: // here arg is casted to A break; case B: // here arg is casted to B break; case C: // here arg is casted to C break; } } ``` A better syntax for this type of switch case might be of order, maybe something like dart-lang/sdk#57173. ``` typedef T is A | B | C; ... void foo(T arg) { switch(arg) { case A -> ... // here arg is A case B -> ... // here arg is B case C -> ... // here arg is C } } ``` This would be a powered down version of OCaML and F# `match <arg> with` as I've not included a proposition for type deconstruction, which would probably require tuples (or more in general product types) as discussed in dart-lang/linter#68.

Posted by GregorySech on Nov 5, 2018, 6:02 AM

Tags

Related issues

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

reliability (E-commerce)

Sum/union types

GregorySechNov 5, 2018, 6:02 AM

[Edit by eernstg: I removed 'type matching' from the title because Dart has pattern matching today.] I'd like to import some features from functional languages like OCaML and F#. The main feature I'm proposing is the possibility of using `typedef` to define sum types. They can be thought of as type enumerations. This will, in some cases, reduce the need for `dynamic` making for sounder typing. For example `json.decode` would no longer return `dynamic` but JSON a sum type, something like: ``` typedef JSON is Map<String, JSON> | List<JSON> | Literal; ``` Sum types could power a new behaviour of switch case that resolves the type based on the typed enumerated. Kind of like `assert` and `if` does with contemporary `is` syntax. ``` typedef T is A | B | C; ... void foo(T arg) { switch(arg) { case A: // here arg is casted to A break; case B: // here arg is casted to B break; case C: // here arg is casted to C break; } } ``` A better syntax for this type of switch case might be of order, maybe something like dart-lang/sdk#57173. ``` typedef T is A | B | C; ... void foo(T arg) { switch(arg) { case A -> ... // here arg is A case B -> ... // here arg is B case C -> ... // here arg is C } } ``` This would be a powered down version of OCaML and F# `match <arg> with` as I've not included a proposition for type deconstruction, which would probably require tuples (or more in general product types) as discussed in dart-lang/linter#68.

Comments (0)

No comments yet. Start the discussion.

Comment on this issue