/proposal-enum

ADT enum proposal for ECMAScript

Primary LanguageHTMLMIT LicenseMIT

Enum proposal

Champion group & Authors

Motivation

  1. Provide enum (Enumerated type) in JavaScript.

    • This pattern is widely used in JavaScript.
  2. Provide Abstract Data Type based on enum.

    • It is implemented in many languages in different names like "enum variants" (Rust), "Data Constructor" (Haskell) or "tagged union" (TypeScript).

Compare to the previous proposals

Roadmap, design goal and relationship to the prior proposals #2

Please go to discussion forum for design details!

https://github.com/rwaldron/proposal-enum-definitions https://github.com/rbuckton/proposal-enum

The current proposal doesn't have a detailed design to be compared with. Currently I'm focusing on figuring out the design constraints before any semantics or syntax.

Design

Before the concreate design, we should decide the design constraints first.

Normal enum

  • Enum should have support for symbol, number, string and bigint.

ADT enum

  1. work well with TypeScript.
  2. work with structs proposal (constraint by @rbuckton and @syg), normal objects and Record & Tuples (for consistency).
  3. work well with pattern matching proposal.

Decided

  • Enum is a frozen, non-extensible normal object
  • Enum is a Declaration
  • Support number, bigint, string, and symbol
  • Support bitflag pattern

Decided not to

  • Allow duplicated keys
  • Hoistable declarations

To be decided

enum A { Case1 }
enum B extends A { Case2 }

B.Case1
enum A { A = 1, B = "a" }
enum A { B = 1 }
A.B === 1
some API to get the name of 1 === "B"
enum A { ["a" + "b"]: 1 }
enum A { B = expr() }
enum A { A = 0, B }; A.B === 1
enum A {} // valid TypeScript and flow.js today
enum A of symbol {} // valid flow.js today
enum A { B }; typeof A.B === ?
enum A { A, B, C }; for (const [key, value] of A);