4 ms·
I loved Ocaml but now I love TypeScript more. It's got about the same amount of type safety, the ergonomics are better to me, and the packaging and ecosystem to
by yujzgzc 11mo ago
I loved Ocaml but now I love TypeScript more. It's got about the same amount of type safety, the ergonomics are better to me, and the packaging and ecosystem tooling are leaps and bounds above anything else.
- phplovesong 11mo agoYou would not really build the same software tho.
- abathologist 11mo ago> It's got about the same amount of type safety What?
- yujzgzc 11mo agoBoth languages have robust and expressive type systems. My experience is that TypeScript's is also more flexible. In Ocaml everything is cool as long as you stick with the functional programming style. But every "interesting" program also has imperative, non-functional-programming parts, and TypeScript has really good automatic "type narrowing" features that make that part much safer in my experience. In Ocaml however, type narrowing isn't automatic at all.
- abathologist 11mo agoTypeScript certainly has a more complicated and flexible type systems in many respects, but it is not the same w/r/t safety. It is quite common to run across `any`s all over the place in TypeScript code, and there is no such thing in OCaml. TypeScript's systems is explicitly unsound (i.e., not fully type safe) by design: https://www.typescriptlang.org/docs/handbook/type-compatibility.html#a-note-on-soundness https://www.typescriptlang.org/docs/handbook/type-compatibil...
- yujzgzc 11mo agoOcaml has exactly the same kinds of escape hatches, like Obj.magic or unsafe accessors. The way I see it. it's a matter of community practice more than language capabilities. Typescript in practice has has the safety net of being interpreted rather than compiled, so I guess people tend to abuse its type flexibility more. But if you write without the escape hatches in both languages, in my experience the safety is exactly the same and the cost of that safety is lower in TypeScript. A very common example I've encountered is values in a const array which you want to iterate on and have guarantees about. TypeScript has a great idiom for this: ``` const arr = ['a', 'b'] as const; type arrType = typeof arr[number]; for (const x of arr) { if (x === 'a') { ... } else { // Type checker knows x === 'b' } } ``` I haven't experienced the same with Ocaml
- debugnik 11mo agoFirst off, the escape hatches in TypeScript are way too accessible compared to OCaml: `JSON.parse(...) as MyInterface` is everywhere and completely broken. And second, you're dismissing the fact that TypeScript is unsound, even worse it is so by design. Easy examples: uninitialized variables holding undefined when their type says they can't [1]; array covariance [2]; and function parameter bivariance, which is part of the TypeScript playground's own example on soundness issues [3] but at least this one can be configured away. C# and Java made the same mistake of array covariance, but they have the decency of checking for it at runtime. [1]: https://www.typescriptlang.org/play/?#code/DYUwLgBAHgXBB2BXAtgIxAJwNwFgBQAxgPbwDOkwRA5hALwQAUAlHQHwTFlGgB0lVDKE1x5+zEUA https://www.typescriptlang.org/play/?#code/DYUwLgBAHgXBB2BXA... [2]: https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAllApgWwgLhmArsgRogJwG0BdGAXhiIGYSBuAKFEllwJAGtEwBJJVDAApseQgB9oBOGADmASlIV4-CIzadufFBAB0ABywQAFoIBEAMxAhTcxs2iYQUAHI58BDCPdKE2ogEZ6JnAIEAAbRB0wkBlhJ1dRAlsGIA https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAllApg... [3]: https://www.typescriptlang.org/play/?strictFunctionTypes=false&q=208#code/PTAEHUEsBcAsHsCu1QENQCNUGMDWBzAJyQDsATUSE0aATwAcBTG2R+Q2gGlFqQHJCzRCQA2kXIxG0AUCBrxMzAGaoAtpDGpCoAO4xYLZpDKN08JWhoNmAZ1o3ojVYqr5QAIhuky7gHTTZMABlbxJGGxtKSLgjE3Q4VBQY0Gx4VXoNRm1sVGpVVAlQfEQtXMdwwLQMJCTWKyZLADdUEURmWFRIxNBCYWhIVUZuXIoSeBQAK0QHSrJEQlcUtIyRRMh4El9QABVYSEj90DHCfJFQJXZQVXgHUHpiIjV1EnxK1ZeS-HCWbq1mDEQGhQejg9W+SmIzjIqFooA2jH8lQAQoCRGRFvFrKA7A4nLo9tgDNcTCIuqB3sVUF98ZBCaAOjZKowAB77RwUC7adBKRg6UAmHImSIIHSMRpZRSpQZdarIK4FVyzRjYfbrEiREEGaCEVAmAC05iUkQ2LEEzAAjiUxP1wgAuUBBAb0MQquicSoAVRsqAwGhgsJGDtC4RsiLkUFB22sQWwC3o0D4kXw8BacIsGEYGIwImY0AUNkQ9Ho7BQLTOAClUM0Y3HoJVUiZuMkKZ9c4xyNF4DotGRIjYnS7-WhyKBpj6-XRKjpWNR7uF2+zdPpd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8QAKypYc054LOgdC+COilaAFJfDsHwMAMgA0bDAAnmQao15gDPSlFUyAvprS0F8PZ1QIggA https://www.typescriptlang.org/play/?strictFunctionTypes=fal... As for your example, I agree that TypeScript unions and singleton types are powerful, but I can't see what are you speficially missing here that pattern matching and maybe an additional variant type doesn't get you in OCaml. You get exhaustiveness checking and can narrow the value down to the payload of the variant.