hkust-taco / mlscript

The MLscript programming language. Functional and object-oriented; structurally typed and sound; with powerful type inference. Soon to have full interop with TypeScript!
https://hkust-taco.github.io/mlscript
MIT License
175 stars 27 forks source link

Add enumerated types (not ADTs) #158

Open LPTK opened 1 year ago

LPTK commented 1 year ago

The original idea of enumerated types is to name some constants of an underlying type and treat these as cases of a new nominal type.

enum Status: Int {
  On = 1
  Off = 0
}
let x: Status = Status.On
let y: Status = 0 // error
x + 1 // allowed because the `: Int` annotation publicizes the relationship

We could also generalize this so that constructors can not range over single values but whole sets of values, possibly filtered by a predicate (for when we have logical refinement types).

(This is different from the way recent languages use enum to define brand new nominal ADTs, which seems like a misappropriation of the original concept/name, probably from the original sin of Java's enum classes.)