[generics] Maybe possible to use <> instead of () as type parameter and

148 views
Skip to first unread message

Xie Zhenye

unread,
Jun 17, 2020, 12:02:03 AM6/17/20
to golang-nuts
In the draft, it says:
"Why not use the syntax F<T> like C++ and Java?
When parsing code within a function, such as v := F<T>, at the point of seeing the < it's ambiguous whether we are seeing a type instantiation or an expression using the < operator. Resolving that requires effectively unbounded lookahead. In general we strive to keep the Go parser efficient."

Maybe adding type metadata to an identifier during parsing, and using the metadata of an identifier may resolve the ambiguous of type parameter or less operator.   For example, '<' after a variable or constant name is a less operator and '<' after a function or type name is a type parameter.

<> will be more readable than () and easier to identify type parameter and function call.

Ian Lance Taylor

unread,
Jun 17, 2020, 12:08:16 AM6/17/20
to Xie Zhenye, golang-nuts
Let's try using this syntax for a while before we discard it. Thanks.

That said, it's a key feature of Go that it is possible to parse a
program without requiring any type information. That is how tools
like gofmt work. We must be able to parse Go code without building a
symbol table. So the suggestion of adding type metadata to an
identifier during parsing is not a choice we are going to take.
Thanks for thinking of it, though.

Ian
Reply all
Reply to author
Forward
0 new messages