escalier-lang / escalier

A compile-to-JavaScript language with tight TypeScript integration.
https://escalier-lang.github.io/escalier/
MIT License
21 stars 1 forks source link

Readonly types #691

Open kevinbarabash opened 1 year ago

kevinbarabash commented 1 year ago

Escalier introduces the idea of mutable references. By default an immutable reference of type T will be equivalent to type Readonly<T>.

An object type T can contain a mix of readonly and mutable fields. A binding mut x: T allows x to mutate all fields except readonly fields and call all methods on T including those with mut self: Self as the receiver. In TS this would mean that Readonly<Set<T>> == ReadonlySet<T>, Readonly<Map<T>> == ReadonlyMap<T>, etc. which currently isn't the case.

Readonly<T> makes all properties readonly and removes all methods with mut self: Self as the receiver. This means that T is assignable to Readonly<T> (aka T is a subtype of Readonly<T>).

Given the binding x: T, i.e. x is not mutable, typeof x should return Readonly<T>.