TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Go generics may use square brackets [] not parenthesis ()

172 点作者 psxuaw将近 5 年前

28 条评论

jiggawatts将近 5 年前
Why don&#x27;t they just use characters from the Canadian Aboriginal Syllabics block?<p><a href="https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;5penft&#x2F;parallelizing_enjarify_in_go_and_rust&#x2F;dcsgk7n&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;5penft&#x2F;parallelizing_...</a>
评论 #23843732 未加载
评论 #23840692 未加载
评论 #23840652 未加载
评论 #23845583 未加载
评论 #23843718 未加载
评论 #23841709 未加载
chmod775将近 5 年前
&gt;A typical computer keyboard provides four easily accessible pairs of single-character symmetrical &quot;brackets&quot;<p>&quot;falsehoods programmers believe about keyboards&quot;<p>On the keyboard layouts of many countries these keys aren&#x27;t accessible at all, either missing entirely or only being accessible via unergonomic key combinations.<p>For this reason myself (and many other programmers) own additional ANSI-US layout keyboards.
评论 #23842970 未加载
评论 #23840654 未加载
评论 #23840655 未加载
评论 #23843266 未加载
评论 #23840741 未加载
评论 #23844279 未加载
评论 #23846176 未加载
评论 #23840693 未加载
评论 #23848266 未加载
评论 #23840583 未加载
评论 #23840603 未加载
评论 #23840718 未加载
rspeele将近 5 年前
I really like the syntax used to declare generic type parameters to functions in ML-type languages like F#.<p>Writing the below in other languages (pseudocode) feels repetitive in the same way that `Thing thing = new Thing()` did back before `var` or `auto` types became popular:<p><pre><code> list&lt;Y&gt; Map&lt;X, Y&gt;(list&lt;X&gt; inputs, X-&gt;Y mapping) </code></pre> Why do I need to put &lt;X, Y&gt; in brackets after Map? To tell the compiler that these are generics, not attempts to reference actual specific types that just happen to have crappy, nondescript names like X and Y.<p>In F# and friends, there is no need to put a bracketed block declaring which types are generic parameters. I would simply write:<p><pre><code> let map (inputs: list&lt;&#x27;x&gt;) (mapping: &#x27;x -&gt; &#x27;y) : list&lt;&#x27;y&gt; = ... </code></pre> The little apostrophe or tick mark before the type name tells the compiler it&#x27;s a generic type parameter. It&#x27;s not legal to name a concrete type with a leading tick mark, so there can be no mix-up. At first it looks ugly, but once you are used to it, it is natural and less cluttered than having to have these separate bracketed declarations for &quot;uh-oh, here comes a generic&quot;.<p>But I suspect Go will always want more explicit syntax for generics. In part out of backwards compatibility, but largely because Go&#x27;s designers see generics as an advanced, occasionally useful feature. Correspondingly they&#x27;ll want the presence of a generic function to have ugly syntactical warning signs: here be dragons.<p>Functional languages usually take the opposite approach and see a fully generic function as <i>less</i> complex than one for a specific type, because just by looking at its signature, you know it doesn&#x27;t require anything special about the input objects it&#x27;s going to work on. It is not going to be calling methods on them or messing with their guts. It&#x27;s just treating them as black-box, could-be-anything cards to be shuffled around from point A to point B.
评论 #23841372 未加载
评论 #23843776 未加载
评论 #23840948 未加载
ca_parody将近 5 年前
To me, Go feels condescending to write without generics. I may just not groke the idioms - but putting users in a walled garden that the standard library has the privileged to escape from (i.e. (Map&lt;k, v&gt;)) just seems to go too far in not trusting the programmer. The fact that generics have taken so many years - to still be talking about what [&lt;( to use is beyond me.<p>There is a difference between becoming C++ and allowing programmers to make fundamental abstractions without interface{} hacks.
评论 #23843524 未加载
评论 #23843288 未加载
评论 #23843274 未加载
hota_mazi将近 5 年前
&gt; A typical computer keyboard<p>A typical NORTH AMERICAN computer keyboard.<p>Does the myopia of the Go compiler team know any bounds?
评论 #23841319 未加载
评论 #23841297 未加载
评论 #23843501 未加载
marcrosoft将近 5 年前
I wish they would just freeze the language and not add anything. I like it the way it is. I feel like the general attitude of software developers is: if it isn’t changing and being updated it’s dying. I think we should start putting weight on stable unchanging software.
评论 #23840482 未加载
评论 #23841043 未加载
评论 #23840476 未加载
评论 #23840689 未加载
评论 #23840970 未加载
评论 #23840714 未加载
评论 #23840481 未加载
评论 #23840715 未加载
评论 #23841782 未加载
评论 #23840918 未加载
评论 #23840704 未加载
评论 #23843916 未加载
评论 #23846571 未加载
评论 #23840596 未加载
评论 #23840537 未加载
shirro将近 5 年前
I really like the Go standard libraries. I can do most of what I need with them. I worry some of the people calling for generics will move the language in the direction of massive abstract libraries. I don&#x27;t want to include a Go version of Boost in everything I build. Complicated type signatures seem like an unnecessary additional burden for average programmers.<p>I really hope generics, like reflection, are only used when there isn&#x27;t a simpler option. I honestly would have preferred and had more use for sum types.<p>I like the square brackets though. They look right for Go and it is more readable.
评论 #23840749 未加载
verandaguy将近 5 年前
Honest question; why use a comparatively exotic syntax for generics? Most other similar languages — Java, C# — and perhaps most notably C++ since its the closest analogue in terms of paradigm and domain, all use angle brackets (without getting into the semantic differences between &quot;classic&quot; generics and the STL).<p>AFAIK, Golang assigns no special syntactic meaning to angle brackets beyond comparison operators (correct me if I&#x27;m wrong though, I don&#x27;t write much Go), and using a familiar syntax would make it easier to work with coming from other languages.
评论 #23841473 未加载
评论 #23841997 未加载
评论 #23841447 未加载
ivoras将近 5 年前
&gt; Angle brackets require unbounded parser look-ahead or type information in certain situations<p>So.... they&#x27;d rather pull the &quot;it&#x27;s hard to do&quot; or the NIH card, than make it easier for developers switching between languages? Interesting choice. Someone mentioned &quot;condescending&quot; -- that description fits perfectly.<p>And there I was thinking that COMPILER WRITING should be hard - as hard as possible - in order to make the code which is to be compiled as easy to write as possible.
评论 #23850746 未加载
thenanyu将近 5 年前
If the type keyword needs to be added anyways, why not angle brackets then? Is it still ambiguous?
评论 #23841163 未加载
wbl将近 5 年前
Just use guillamets already.
评论 #23840602 未加载
emmelaich将近 5 年前
I know it&#x27;s unpopular but judicious use of significant white space would solve the ambiguity too.
rurban将近 5 年前
Perl allows optional delims if ambigious. so if the best set &lt;&gt; is ambigious double or triple them. But much easier would be to demand uppercase T type variables.<p><pre><code> a, b = w &lt; x, y &gt; (z) </code></pre> w is lowercase, unambigious. Parsed as list of comparisons.<p><pre><code> a, b = T &lt; x, y &gt; (z) </code></pre> Ambigious, so use<p><pre><code> a, b = T &lt;&lt;&lt; x, y &gt;&gt;&gt; (z) </code></pre> instead. These cases are very rare. Or demand parens for such ambigious lists of comparisons. Might be too late already.<p>Of just demand the type keyword there also:<p><pre><code> a, b = w &lt; type x, y &gt; (z)</code></pre>
pjmlp将近 5 年前
If they finally end up adding any kind of generics support, it feels much more natural, from my experience with languages that use [] and () for type parameters, [] are much better visually.
tapirl将近 5 年前
This is a good move, which will make Go more consistent. Making custom generic and builtin generic consistent absolutely makes Go more easy to learn and read.
kitd将近 5 年前
I liked the alternative proposed later in the thread:<p><pre><code> gen T type pair struct { a, b T } &#x2F;&#x2F; contrast with type pair(type T) ... gen T,U type W struct { a T; b U } &#x2F;&#x2F; contrast with type W(type T, U) ... gen T func Print(s []T) {...} &#x2F;&#x2F; print a slice of T </code></pre> or using `forall`<p><pre><code> forall T, type pair struct { a, b T } </code></pre> V readable IMO
deanCommie将近 5 年前
&gt; Angle brackets require unbounded parser look-ahead or type information in certain situations<p>I wonder how this is solved in Java, and C++, and C#, and...
评论 #23840503 未加载
评论 #23840521 未加载
评论 #23841461 未加载
评论 #23840501 未加载
评论 #23840504 未加载
评论 #23840497 未加载
评论 #23840494 未加载
评论 #23840498 未加载
评论 #23840492 未加载
alvarelle将近 5 年前
I did not see digraphs mentioned. Could look like &lt;: and :&gt;.<p>The formatting tool could even normalize that to some prettier unicode.
hedora将近 5 年前
Are they adding Java style generics (type erasure), or C++ style (compile time specialization)?
评论 #23841073 未加载
评论 #23840768 未加载
_ph_将近 5 年前
Honestly, I prefer the parenthesis notation to the square or angled brackets. It is perhaps a bit verbose, but easy to read in my eyes. Not to mention the keyboard problems with non-English keyboard layouts.
chvid将近 5 年前
Doesn&#x27;t [ ] have similar problems? Suppose I write:<p>a[x](z)<p>Is that a read of array a with index x followed by a function invocation with the parameter z.<p>Where it could also be a type conversion of z into generic type a of x.
评论 #23845352 未加载
coldtea将近 5 年前
Funny how we add 100 new emoji per year, and yet we can&#x27;t seem to be able to add a new matching pair of characters in our keyboards for 5+ decades...
评论 #23841438 未加载
评论 #23840762 未加载
jfoutz将近 5 年前
seems like you could just not use &#x27;,&#x27; as the separator character in type declarations. +-*^% I think have the same problems, but @#$ are available. weird syntax, but go already makes the tradeoff, do what&#x27;s fast to parse.
nemothekid将近 5 年前
You have emoji on your keyboard?
评论 #23841652 未加载
IshKebab将近 5 年前
This is much more readable IMO.
arun6582将近 5 年前
Whatever!! Go finally brought generics. The same thing which it used to criticize java for Lol
tenderfault将近 5 年前
can someone just stick this thread forever on top of HN listing
Chloro将近 5 年前
If go gets generics I will never use it again.
评论 #23843594 未加载
评论 #23845561 未加载
评论 #23841331 未加载