Jack-Works / proposal-enum

ADT enum proposal for ECMAScript
MIT License
89 stars 1 forks source link

Differences between ADT and tagged union #28

Open legendecas opened 2 years ago

legendecas commented 2 years ago

In the README, it is said that ADT is expected to be similar to the tagged union type in TypeScript. However, the tagged union type is just plain objects in JavaScript and we can already use it nowadays, and the type system is built with TypeScript and there is nothing to do with JavaScript: TypeScript emits legit JavaScript codes and stripped type information in .js results.

It is also noted that the ADT is expected to work with pattern match well. However, I didn't find there is significant gain with ADT compared with the tagged union pattern on the pattern match with a new significant type introduction:

  1. With ADT (there is nothing described in the README about ADT, so I'm assuming the shape described at https://github.com/Jack-Works/proposal-enum/discussions/26#discussioncomment-1668178):
    
    enum Message for function {
    Quit,
    Move { x, y },
    Write(message),
    }

const value = Message.Write('my message'); match (value) { when MessageCreator.Write(message): ... }

if (value.type === Message.Write) { ... }


2. With the tagged union pattern: (copied from https://github.com/tc39/proposal-pattern-matching#code-samples)
```js
const Message = {
  Write: 'write',
}

const value = { type: Message.Write, message: 'my message' };
match (value) {
  when ({ type: Message.Write, message, ...rest }) {
    handleMessage(message, rest);
  }

  else { throwSomething(); }
}

Comparing the above two examples, I didn't find significant benefits we gain from introducing a new "type" to the language. It will be helpful if the proposal can describe the concrete benefits of the new concept.