Skip to content

Jack-Works/proposal-enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);