News for hackers. Stuff that matters.
Re:Phantom Types in Gleam by pd-andy on Saturday February 27, 2021 @10:41AM (#26285514)
Not quite. I guess in some ways phantom types achieve the opposite of structural typing.

In a language with structural typing, two types are considered equivalent if they share the same structure. With phantom types we can have a single structure and disambiguate between different uses without touching the underling structure. There's no difference between Id(User) and Id(Post) at runtime, the annotation is purely a compile-time restraint.

In fact, because of typescript's structural typing, the only way to use phantom types in ts is to have some dummy field of type `never` that the type system can use to disambiguate between the two [1].

[1]: https://gist.github.com/GoNZooo/243b23702df1fae38c966ae18832...

Re:Phantom Types in Gleam by mushishi on Saturday February 27, 2021 @11:49AM (#26286025)
Holy smokes, that would be irritating :D

Btw. is there a difference between these two:

    (s: string): PlainText => <PlainText><any>s;
    (s: string): PlainText => s as any as PlainText;
Syntax-wise I'd rather use the latter for it's more clear in typed JSX-context.
Re:Phantom Types in Gleam by pd-andy on Saturday February 27, 2021 @11:51AM (#26286041)
As far as I know those two are equivalent. Definitely slightly clunkier, but is what it is.
Re:Phantom Types in Gleam by timdeve on Saturday February 27, 2021 @12:06PM (#26286179)
Yes, exactly, it's because of Structural Typing that you have to implement Phantom Types in that hacky way rather than Structural Typing being the "trick".