3 ms·
The article describes making "number" a different type, not A and B. It's true that making A and B different is a unique problem of TypeScript, but making numbe
by quonn 2y ago
The article describes making "number" a different type, not A and B. It's true that making A and B different is a unique problem of TypeScript, but making number a different type is a common issue in many languages.
- DanielHB 2y agonumber is a primitive, branding a primitive can be done like in the example. To brand a class you could also add a private field. Some languages all values are objects and in those languages then the branding argument applies the same way. For languages with nominal typing and primitives you need to box the type yes. Regardless the core of the issue is understanding how structural typing works vs nominal typing
- dunham 2y ago> For languages with nominal typing and primitives you need to box the type yes. But the compiler can elide the box for you. Haskell and Idris do this. Haskell's newtype gives a nominal wrapper around a type without (further) boxing at at runtime. It is erased at compile time. Haskell does box their primitives, but via optimization they are used unboxed in some cases (like inside the body of a function). This technique could be applied to a language that doesn't box its primitives. Idris also does this for any type that is shaped like a newtype (one data constructor, one argument). In that case, both on the scheme and javascript backend, a newtyped Int are represented as "bare" numbers. E.g. with: type Foo = MkFoo Int a `MkFoo 5` value is just `5` in the generated javascript or scheme code.