TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Union Types in Flow and Reason

109 pointsby z1mm32m4nabout 7 years ago

15 comments

esprehnabout 7 years ago
The TypeScript comment is wrong, the author needs to use a const enum: <a href="https:&#x2F;&#x2F;www.typescriptlang.org&#x2F;docs&#x2F;handbook&#x2F;enums.html#const-enums" rel="nofollow">https:&#x2F;&#x2F;www.typescriptlang.org&#x2F;docs&#x2F;handbook&#x2F;enums.html#cons...</a><p>Then it generates code that&#x27;s better than Flow. It&#x27;s not as aggressive as Reason for sure, but it&#x27;s still pretty good.<p><a href="https:&#x2F;&#x2F;www.typescriptlang.org&#x2F;play&#x2F;#src=const%20enum%20ScreenType%20%7B%0D%0A%20%20%20%20LoadingScreen%2C%0D%0A%20%20%20%20CodeEntryScreen%2C%0D%0A%20%20%20%20SuccessScreen%2C%0D%0A%20%20%20%20FailureScreen%2C%0D%0A%7D%0D%0A%0D%0Aconst%20impossible%20%3D%20(x%3A%20never)%3A%20never%20%3D%3E%20%7B%0D%0A%20%20throw%20new%20Error(&#x27;This%20case%20is%20impossible.&#x27;)%3B%0D%0A%7D%0D%0A%0D%0Aconst%20needsCancelButton%20%3D%20(screen%3A%20ScreenType)%3A%20boolean%20%3D%3E%20%7B%0D%0A%20%20switch%20(screen)%20%7B%0D%0A%20%20%20%20case%20ScreenType.LoadingScreen%3A%0D%0A%20%20%20%20%20%20return%20true%3B%0D%0A%20%20%20%20case%20ScreenType.CodeEntryScreen%3A%0D%0A%20%20%20%20%20%20return%20true%3B%0D%0A%20%20%20%20case%20ScreenType.SuccessScreen%3A%0D%0A%20%20%20%20%20%20return%20false%3B%0D%0A%20%20%7D%0D%0A%20%20return%20impossible(screen)%3B%0D%0A%7D%0D%0A" rel="nofollow">https:&#x2F;&#x2F;www.typescriptlang.org&#x2F;play&#x2F;#src=const%20enum%20Scre...</a>
评论 #16901774 未加载
评论 #16900219 未加载
评论 #16900181 未加载
评论 #16900733 未加载
simplifyabout 7 years ago
ReasonML is by far my favorite emerging JavaScript tech. Not only is its JS code output small, it&#x27;s also performant. For immutable updates it generates code faster[1] that Object.assign! :)<p>[1] <a href="https:&#x2F;&#x2F;medium.com&#x2F;@javierwchavarri&#x2F;performance-of-records-in-bucklescript-ecf6566fff5c" rel="nofollow">https:&#x2F;&#x2F;medium.com&#x2F;@javierwchavarri&#x2F;performance-of-records-i...</a>
评论 #16906960 未加载
edjrootabout 7 years ago
As someone who 1) knows a few functional programming concepts, 2) hasn&#x27;t programmed extensively in a functional language, 3) would like to get productive fast (in web development), but also 4) wants a language for the long term (think general purpose, decent momentum), including for academic interests... Is there a language that fits all these requirements, or am I asking too much?<p>Elm is too limited, Haskell seems too convoluted, unsure about F# and OCaml, don&#x27;t know about good alternatives.<p>I&#x27;ve been considering PureScript because it seems to strike a balance between features, JS interoperability and skill transferability, but I&#x27;m still a little afraid of it being unnecessarily complex for my next short-term projects.<p>I&#x27;ve only now started considering ReasonML, but am I wrong to think it&#x27;s much closer to the &quot;get productive fast&quot; side than the &quot;long-term investment&quot; one? Not to mention its JS-like syntax, which I admit I have a distaste for.
评论 #16900996 未加载
评论 #16900726 未加载
评论 #16901242 未加载
评论 #16900841 未加载
评论 #16900858 未加载
评论 #16902797 未加载
评论 #16910149 未加载
评论 #16903825 未加载
评论 #16900982 未加载
评论 #16903086 未加载
评论 #16902707 未加载
评论 #16900877 未加载
评论 #16900797 未加载
orfabout 7 years ago
How is the uglify.js example correct? It replaces &#x27;status !== 2&#x27; with &#x27;!(status&gt;=2)&#x27;<p><pre><code> &gt; needsCancelButton(2) false </code></pre> Surely it should be:<p><pre><code> function needsCancelButton(n){ return 2 === n } </code></pre> Also, more generally, uglify.js has no idea about the range of integers that could be passed in so how is a greater than optimization like that ever valid?
评论 #16899794 未加载
评论 #16899645 未加载
telabout 7 years ago
Nitpicky but critical detail: union types are not the same as &quot;sum&quot; types such as what you get in Reason, Purescript, and Elm. Union types are convenient for adding atop a dynamic language or modeling records (although, Purescript&#x27;s row types are better), but they are less capable of abstraction.<p>The key example to differentiate union and sum types is `int union int == int` whereas `int sum int` is something unique and different from `int`. A sum type keeps track of the &quot;left side&quot; versus the &quot;right side&quot;.<p>So, all said and done not only is Reason producing faster, tinier code... it&#x27;s also managing a more sophisticated and powerful abstraction!
评论 #16903044 未加载
djurabout 7 years ago
The difference between Reason&#x27;s and PureScript&#x27;s output is easily explained if you take into account the differing goals of the two projects. PureScript has an emphasis on [JS interop](<a href="https:&#x2F;&#x2F;leanpub.com&#x2F;purescript&#x2F;read#leanpub-auto-polyglot-web-programming" rel="nofollow">https:&#x2F;&#x2F;leanpub.com&#x2F;purescript&#x2F;read#leanpub-auto-polyglot-we...</a>) going both ways. Being able to add some PureScript code to a JS codebase, or even to intentionally layer a JS frontend on top of a PureScript core, is an explicit design goal.<p>You can actually create instances of PureScript types in JS and pass them into functions defined in PureScript. That explains why there&#x27;s a JS class for each data constructor. Similarly, the generated PureScript code can&#x27;t actually &quot;know[] the match is exhaustive&quot;, since it could be called from anywhere.<p>The article was interesting, but I think the comparison would benefit from providing an example of the code generated for calling the function as well as its definition.
jmhainabout 7 years ago
From the section on TypeScript:<p>&gt; enums, which are basically like Java’s enums.<p>They really aren&#x27;t. Java enums are somewhat unique in that they are full-fledged classes, not just glorified constants. TS enums are much closer to those of C#.
andyonthewingsabout 7 years ago
Haxe also produces pretty nice JS output:<p>var needsCancelButton = function(screen) { switch(screen) { case 0:case 1: return true; case 2: return false; } };<p>The Haxe code can be found here: <a href="https:&#x2F;&#x2F;try.haxe.org&#x2F;#79e40" rel="nofollow">https:&#x2F;&#x2F;try.haxe.org&#x2F;#79e40</a>
z3t4about 7 years ago
Meanwhile in dynamic land:<p><pre><code> if(cancelButton == undefined) throw new Error(&quot;Specify if there should be a cancel button or not&quot;)</code></pre>
lnrdgmzabout 7 years ago
If the Flow type checker ensures exhaustiveness, why does the switch statement require a default case and an `impossible` function?
评论 #16901394 未加载
评论 #16929338 未加载
z3t4about 7 years ago
I think the root of the problem is overzealous use of OOP patterns like inheritance. If you would do the most naive implementation you would just do one view, copy it, and when needed a fourth, just copy it again, instead of entangling your code with yet more paths.
yawaraminabout 7 years ago
This is a nice insight into Reason&#x27;s handling of sum types. Here&#x27;s the Reason code as we&#x27;d typically write it:<p><pre><code> let needsCancelButton = fun | LoadingScreen | CodeEntryScreen =&gt; true | SuccessScreen =&gt; false };</code></pre>
FeepingCreatureabout 7 years ago
Are union types the new lambdas?
评论 #16901504 未加载
icebrainingabout 7 years ago
For the Appendix: Idris is also a functional language that can compile to JS. I wouldn&#x27;t say the output is very readable, but except for a couple of JS interop issues, I haven&#x27;t had too look at it at all :)
komuWabout 7 years ago
I wonder how the dart to js compiler would do