Design decision for ADT support #19
Replies: 3 comments 3 replies
-
I feel like this needs a lot of refinement and even then might be difficult to make it feel ergonomic while providing commonly expected features. The biggest problem I have is that there's practically a nerfed function definition for the constructors. It feels like people will try to have default function arguments for the constructed functions. They would also be trying to have custom implementations for the constructor functions. My question then becomes: why wouldn't they be allowed? And if they are allowed, I don't see much point in having a different syntax for their definition. Extending What I'm most worried about is that coders might compromise their flexibility and end up writing more code elsewhere in order to have all constructors fit that narrow mold. For most people writing action creators for stuff like Redux, it's common that we have a ton of boilerplate functions of form |
Beta Was this translation helpful? Give feedback.
-
My suggestion would be to restrict the ADT design to just functional forms I would possibly even restrict the ADT case to singular values as well - Apart from that, the overall functional design makes sense to me. @Jack-Works what can be done to progress the design in the proposal further? |
Beta Was this translation helpful? Give feedback.
-
In the pattern matching proposal we have discussed removing object extractors (i.e.,
As a result, I've been considering an ADT enum syntax that only leverages the functional form, but allows for object binding patterns as well:
Here, const msg = Message.Move({ x: 1, y: 2 });
msg instanceof Message.Move; // true
msg.x; // 1
msg.y; // 2 Yet still allow the extractor syntax that matches the declaration:
|
Beta Was this translation helpful? Give feedback.
-
Algebraic data type, it is implemented in many languages in different names like "enum variants" (Rust), "Data Constructor" (Haskell) or "tagged union" (TypeScript).
This proposal is inspired by the Rust style, which added the tagged union into the enum itself.
The general idea of ADT support is: after defining a plain enum, we allow to also derive a constructor object for this enum that can contain data.
Here is an example (just for idea explaining, not the exact syntax/semantics).
How does it integrates with pattern matching proposal:
How does it integrate with TypeScript:
Q&A
Why do we need a constructor object
MessageCreator
for that?Because the enum value itself is a plain primitive value that cannot be called. In a language like Rust, they don't have this problem because their ADT enum members are used as a function, and can only be destructed by pattern matching or
if let
.Why does pattern matching integration match by the
MessageCreator
instead ofMessage
?Pattern matching proposal requires the "custom matcher" to have a
Symbol.customMatcher
property. It's also not possible to put a symbol on the primitive value. The only place I can put it is in the constructor object.Design decisions
To be decided
Beta Was this translation helpful? Give feedback.
All reactions