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
- Huh, this is not my experience.
- For some reason an Azure outage does not faze me in the same way that an AWS outage does.
- The triplet of wind, pole, dragon appears multiple times in the same order, so is possibly a single phrase that has been broken up into t...
- Option to skip refreshing resource instances whose configuration hasn't changed since the last apply
- [YouTube] Unable to extract uploader id
- Add ability to prioritize GitHub Action runners
Raise real-world problems, find people facing the same issue, and discuss solutions.4,000 issues0 comments
reliability (E-commerce)
Huh, this is not my experience.
For some reason an Azure outage does not faze me in the same way that an AWS outage does.
The triplet of wind, pole, dragon appears multiple times in the same order, so is possibly a single phrase that has been broken up into t...
Option to skip refreshing resource instances whose configuration hasn't changed since the last apply
[YouTube] Unable to extract uploader id
Add ability to prioritize GitHub Action runners
Microsoft Flight Simulator (2020) (1250410)
Codegen collaboration with Apollo Codegen and GraphQL Code Generator
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.
Comments (0)
No comments yet. Start the discussion.