Hacker News new | past | comments | ask | show | jobs | submit login

Interesting, thanks for the explanation. See the link at the bottom for what I think foo and bar would be in TypeScript. I'm curious if you see specific weaknesses in it. Seems like maybe this is all just a clash between formalism and how languages work in practice, similar to how it's nice to just pretend that computers work on real numbers instead of floats. In this case, maybe you'd need to pretend that JS works on values rather than having object identity.

> Tags never overlap.

Agreed that TypeScript doesn't enforce this property of the type definition, and it would be nice that it did, although my interpretation of the claim was "any sum type is expressible as a tagged union". I certainly don't claim that TypeScript has a syntax for describing tagged unions and ensuring that they're well-formed; it's just that they're expressible via other features.

> Tags are only used to discriminate between cases.

It's true that tags are string values in my case, although possibly a fancier version could use opaque values. But given that TypeScript enforces that the type is either 'A' or 'B', couldn't any other language derive that string from the case and use it?

Do you view (say) SML datatypes as being valid sum types? I'm having a hard time understanding when SML types would be strictly more powerful/expressive than TypeScript here.

https://www.typescriptlang.org/play/index.html#src=type%20A%...




> Seems like maybe this is all just a clash between formalism and how languages work in practice,

I think you missed the word “broken”, before “languages”. Sums in Standard ML are really sums.

> similar to how it's nice to just pretend that computers work on real numbers instead of floats.

Computers can totally work on real numbers. (Okay, not all of them. But way many more than floats allow.) For example, you can implement an abstract type whose internal representation is Cauchy streams of rationals, and only provide operations that send equivalent streams to equivalent ones. Of course, this would be horribly slow, and few people actually need exact real arithmetic anyway.

> In this case, maybe you'd need to pretend that JS works on values rather than having object identity.

JavaScript does work on values. The values are the object identities.

> See the link at the bottom for what I think foo and bar would be in TypeScript. I'm curious if you see specific weaknesses in it.

You anticipated it yourself:

https://www.typescriptlang.org/play/index.html#src=type%20A%...

> Do you view (say) SML datatypes as being valid sum types?

Yes. On the other hand, Haskell's aren't, albeit for different reasons than the ones given in this thread.

> I'm having a hard time understanding when SML types would be strictly more powerful/expressive than TypeScript here.

In Standard ML, type abstraction actually works. You can hide the implementation of an abstract type, making it impossible for others to destroy the invariants that you worked so hard to establish. Or at least should have.

See here for why union and intersection types make type abstraction difficult: https://news.ycombinator.com/item?id=16399722




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: