5 ms·
Pattern Matching Custom Data Types in TypeScript
- maxxxxx 8y agoI think the code is way too verbose. One supposed advantage of FP languages is that they are more concise. If I have to build a ton of boilerplate I might just as well go to some OOP pattern with interfaces and so on.
- alexsasharegan 8y agoRemember that the point of the article is not what language does it best, but rather how to use typescript to add patterns from other languages.
- maxxxxx 8y agoTrue. I just think it's a mistake to force a pattern into a language that doesn't support it well. In the end you get sort of pattern matching but you have also to write a lot of code.
- sfvisser 8y agoIt's a neat trick and I've used similar patterns in some of our projects, but in the end it doesn't buy you much over a `switch (x.kind) { ... }`. (Except your matcher can be used as a proper expression which is nice obviously) But you don't have proper pattern nesting, fall-through semantics and it's not clear (to me yet) how to extend to matching on multiple values. Try implementing an equality function on a sum-type using pattern matching. Having actual proper pattern matching added to TS would be such a great addition. Especially with full support of the control flow analysis.
- alexsasharegan 8y agoThis might be our best hope right now: https://github.com/tc39/proposal-pattern-matching https://github.com/tc39/proposal-pattern-matching
- Klathmon 8y agoI see a lot of people asking for this in TS, but i'd much rather it be in plain JS and let TS add anything on top of it that is needed. Pattern matching would work SO WELL in javascript, especially considering how frequently switch is currently poorly used as a replacement in things like Redux.
- moogly 8y agoYeah, but there's no way that can be the final syntax, because it's absolutely horrible (TS could always have a Rust/C# 8 one on top though).
- swissmanu 8y agoi did an article about quite a similar solution approach some time ago: https://pattern-matching-with-typescript.alabor.me/ https://pattern-matching-with-typescript.alabor.me/ having real pattern matching built in to TS would be awesome. nice write up though!
- alexsasharegan 8y agoThis article is totally on the same wavelength. I might have to link it as further reading on the subject. Any objections?
- swissmanu 8y agosure! go ahead. thanks :-)
- jaunkst 8y agoYou could use a typescript decorator to make it less verbose to setup I suppose. I would rather have a more native approach.
- deleted 8y ago[deleted]
- deleted 8y ago[deleted]
- mattigames 8y agoI'm disappointed in TypeScript not having pattern matching, multiple classes inheritance, and operator overloading (a MUST for game development)
- dested 8y agoI understand the sentiment, but really you should be disappointed in the ECMA standard, typescript only implements stage 3 and hire.
- Tarean 8y agoSince typescript is compiled anyway it feels somewhat silly not to have the compiler do this work. I wonder how hard it would be to make the desugaring extensible, maybe with a rewrite engine like what Hermit does in haskell.
- PudgePacket 8y agoDon't think the "never" default arm is necessary. If the return type is set and you miss a case TS should complain.