by sizediterable · 5y ago · 0 comments
> Luau’s type system is structural by default
I think that makes it the second (production-ready) language to have a primarily-structural type system, after TypeScript/Flow(/and Closure to some extent) for JS. I wonder if it will follow in their impressive footsteps or if it will tread new territory.
One of the things that Flow gets right that TypeScript doesn't yet support is switching to nominal type-checking between class instances. In TypeScript you can assign an instance of one class to an instance of another as long as they have compatible fields. Compare:
Flow: https://bit.ly/3jZx4rB
TypeScript doesn't have any understanding of prototypes or prototypal inheritance, which is a big gap considering that it's a fundamental aspect of JS.
I'm really hoping Luau will come to do the right thing with tables and prototypes. Perhaps it already does, but unfortunately there doesn't seem to be a Luau playground to try in the browser.
We'd like to afford the same kind of ideas for native Luau code, but we're not there yet.
A regular MOVE is sort-of structural. It moves field i of the source to field i of the destination. Source and destination need not be of the same type, though.
MOVE CORRESPONDING is nominal. It moves field Foo of the source to field Foo of the destination (leaving fields that do not have corresponding items alone)
In both cases, data may be converted, for example from alphabetic to alphanumeric.
To be fair to Typescript, it's an insane fundamental aspect of JS.
# let f x : int = x#foo;;
val f : < foo : int; .. > -> int = <fun>
Actually there is not really a type of "a record that has at least some fields": # type t = < foo : int; .. >;;
Error: A type variable is unbound in this type declaration.
In type < foo : int; .. > as 'a the variable 'a is unbound
Its hidden (row) polymorphism, it's not subtyping but type abstraction. We're implicitely quantifying over the actual type: # type 'a t = < foo : int; .. > as 'a;;
type 'a t = 'a constraint 'a = < foo : int; .. >
For the record (ha!), for people who don't know ocaml here are "structural sum types", or polymorphic variants as we call them. # let x = `Bar;;
val x : [> `Bar ] = `Bar
above: type of `Bar is a sum containing "at least" (polymorphic) `Bar # type int_opt = [ `Some of int | `None ];;
type int_opt = [ `None | `Some of int ]
above: some "exact" nameless sum type, with arguments # type 'a t = [< `Foo | `Bar ] as 'a;;
type 'a t = 'a constraint 'a = [< `Bar | `Foo ]
above: polymorphic type of sums which contain at most these two constructors.refs:
https://dev.realworldocaml.org/objects.html
https://dev.realworldocaml.org/variants.html#polymorphic-var...
I was thinking of how you can embed types in structs. You automatically dereference the fields and methods of the components directly from the outside.
Fun fact: If you add a private field to a class it'll behave nominally in TS. This is because private fields kinda require nominal relations to function. So, in a way, it does support "switching" to nominal type checking for classes - the opt in is simply per-class.
class CovariantFoo<T> {
private phantom!: T
}
class ContravariantFoo<T> {
private phantom!: (_: T) => void
}
class InvariantFoo<T> {
private phantom!: (_: T) => T
}
(inspired by Rust [0])https://www.cl.cam.ac.uk/~jdy22/papers/lightweight-higher-ki...
I assume we are talking about a static type system here? Many common "scripting" languages are structurally typed - what Python calls duck typing.