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.

Types Are Moving to the Right

167 pointsby dmitriy_koabout 6 years ago

24 comments

andolanraabout 6 years ago
There are several technical advantages to the types-to-the-right style. For one, it&#x27;s often easier to write a parser for: having an explicit <i>let</i> or <i>val</i> keyword makes it immediately obvious to a parser without lookahead that the statement in question is a declaration, and not an expression. This is less of a problem in languages like Java, where the grammar of types is simpler, but in C and C++, if you begin a line with<p><pre><code> foo * bar </code></pre> then it&#x27;s not yet clear to the parser (and won&#x27;t be until after more tokens are observed) whether this is a variable declaration for <i>bar</i> or a statement multiplying <i>foo</i> and <i>bar</i>. This isn&#x27;t a problem for name-then-type syntaxes.<p>On a related note, it&#x27;s also often advantageous for <i>functions</i> to indicate their return type last, as well, especially when the return value of the thing might be a function of an earlier argument. There are plenty of examples of this in functional or dependently-typed languages, but even C++ (which historically has listed the return type of a function first) has added an alternate (slightly clunky) syntax for function types where you can specify the return type after the arguments for this reason:<p><pre><code> template&lt;typename Container, typename Index&gt; auto foo(Container&amp; c, Index i) -&gt; decltype(c[i]) { ... }</code></pre>
评论 #19411766 未加载
评论 #19410871 未加载
评论 #19412330 未加载
评论 #19411616 未加载
评论 #19410517 未加载
评论 #19410698 未加载
评论 #19411071 未加载
评论 #19410412 未加载
int_19habout 6 years ago
I don&#x27;t think it has much to do with type inference. It&#x27;s more that type systems became more complicated, and so did type names. And with a long composite type name, the name of the variable gets pushed too far out and obscured. It worked great in Algol, and still works pretty well in C (although that is partly because it splits the declarator to keep array and function syntax to the right of the variable name), but in C++ with templates it&#x27;s already hard to read.<p>There are also a variety of issues with parsing it that way, most of which go away entirely if the name is first.
评论 #19410320 未加载
评论 #19410337 未加载
mjw1007about 6 years ago
Looks to me like the world had mostly settled on types-on-the-right already in the 1970s, except C was an anomaly and languages which imitated its syntax in other ways often imitated that too.
评论 #19411013 未加载
kronaabout 6 years ago
Python type hints (since 3.5) also follow the same pattern. see: <a href="https:&#x2F;&#x2F;www.python.org&#x2F;dev&#x2F;peps&#x2F;pep-0483&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.python.org&#x2F;dev&#x2F;peps&#x2F;pep-0483&#x2F;</a>
评论 #19410578 未加载
majewskyabout 6 years ago
Types are moving to the right because having them on the left makes the grammar undecidable. Languages like C or C++ can only be parsed when semantic information is passed back into the parser.<p>For example, consider the following C++ statement:<p><pre><code> a b(c); </code></pre> This is a declaration of `b`. If `c` is a variable, this declares `b` of type `a` and calls its constructor with the argument `c`. If `c` is a type, it declares `b` to be a function that takes a `c` and returns an `a`.
评论 #19410648 未加载
评论 #19412332 未加载
评论 #19411810 未加载
评论 #19411034 未加载
评论 #19411484 未加载
Aearnusabout 6 years ago
I disagree with the fact that types are moving over to the right side for readability&#x27;s sake or anything like that. Modern theorem proving languages explicitly specify that type declarations are simply equivalent to set inclusion, which makes the type specification operator (usually :) equivalent to set inclusion (∈). This influence was what rubbed off onto modern MLs, and by extension, inspired many of the ML inspired&#x2F;type safe languages that have come out recently.
seanwilsonabout 6 years ago
Type annotations on the right reads way more naturally to me e.g. &quot;customerNameToIdMap is hash map of strings to UUIDs&quot; over &quot;there&#x27;s a hash map of strings to UUIDs called customerNameToIdMap&quot;.
评论 #19410788 未加载
jeyabout 6 years ago
I thought this is from ML and related functional languages? (i.e. this is just the way they&#x27;ve always done it)
评论 #19411025 未加载
dragonwriterabout 6 years ago
Nitpick, but in the graphic at the beginning, C# is presented as being a language designed in the 21st century when it in fact was doing designed in, and released in the last year of, the 20th.
评论 #19416453 未加载
AnaniasAnanasabout 6 years ago
Something that is not mentioned in the article, the mathematical way is that the types are to the right.
评论 #19413301 未加载
rovykoabout 6 years ago
Is there a name for side on which the type declaration sits? Left&#x2F;right typed? If that&#x27;s the case, I&#x27;d argue for back&#x2F;forward or start&#x2F;end to account for right-to-left languages.
评论 #19410388 未加载
评论 #19410608 未加载
评论 #19411478 未加载
评论 #19410873 未加载
评论 #19410906 未加载
评论 #19410542 未加载
richardhodabout 6 years ago
That first chart really annoys me because rather than giving us the names of the languages it uses a logo, which do not really describe what languages are if you do not recognize the logo. And generally it&#x27;s an extra cognitive layer and load on your brain. Makes it pretty unreadable.
externalrealityabout 6 years ago
Interesting observation. Is a wide enough sample of languages considered? What scares me is that we still use text files to program in 2019 such that we are having this conversation. The author makes a good observation nonetheless.
评论 #19413460 未加载
评论 #19412183 未加载
zwetanabout 6 years ago
Let me correct that for you<p><pre><code> That is essentially the way it is done in Scala (2004), F# (2005), *ActionScript 3 (2006)*, Go (2009), Rust (2010), Kotlin (2011), TypeScript (2012), and Swift (2014) programming languages.</code></pre>
评论 #19412625 未加载
zwiebackabout 6 years ago
I can see pros and cons for either but what about<p>int i,j,k;<p>I think that&#x27;s clearer with type first.
评论 #19410479 未加载
评论 #19411060 未加载
评论 #19410302 未加载
craigharleyabout 6 years ago
And php does it both ways with its type hinting...<p><pre><code> function foo(string bar): string { return “y u do this”; }</code></pre>
yonatronabout 6 years ago
&quot;circa&quot;, not &quot;sirca&quot;.
评论 #19412645 未加载
Tooabout 6 years ago
In JavaScript with prototypical inheritance, where you inherit from an instance rather than a type, it becomes very confusing what Foo would mean in a line saying Foo bar. So there some more explicit distinction is needed which is easiest to do with colon and moving to the right.
stevefan1999about 6 years ago
I have a question, are those languages that provide type inference inspired by HMT [1]?<p>[1]: <a href="https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Hindley–Milner_type_system" rel="nofollow">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Hindley–Milner_type_system</a>
评论 #19426870 未加载
trixie_about 6 years ago
So much easier to read and write code with inferred types.<p>Though my co-workers obsessed with writing ‘good’ code refuse to use them. They also refuse to write comments because their code is so good ‘it documents itself’.
评论 #19411027 未加载
评论 #19411084 未加载
评论 #19410886 未加载
评论 #19412153 未加载
chimiabout 6 years ago
VB and Pascal were way ahead of their time.
评论 #19410893 未加载
mrutsabout 6 years ago
I think the modern practice of displaying types mostly comes from ML.
评论 #19422870 未加载
评论 #19444254 未加载
karlakushabout 6 years ago
I&#x27;m not convinced. The article found one situation where it makes more sense for the type to be on the right, but in most situations it makes more sense for the type to be on the left. The reason being that it reads more naturally. It&#x27;s the difference between saying &quot;Golfer Tiger Woods adopted a cat&quot; and &quot;Tiger Woods, golfer, adopted a cat.&quot; Nobody speaks the latter.
评论 #19411082 未加载
评论 #19411017 未加载
评论 #19410577 未加载
htorabout 6 years ago
this article has little or no substance
评论 #19412634 未加载