proposal: Go 2: type and expression super parameters #36497
Labels
FrozenDueToAge
LanguageChange
Suggested changes to the Go language
Proposal
v2
An incompatible library change
Milestone
Introduction of the proposal
This is not a generic proposal (at least not a generic proposal mainly), though it may solve some problems caused by lack of generics.
An
expr
super parameter means a lazy-evaluated value. For example, in the following function,b
andc
will only be evaluated ifa
is evaluated totrue
.An example using
type
super parameters:A
type
super parameter must be an instance of a kind. TheMap
,Slice
,Channel
,String
,Numeric
andAny
used in the above example are some pre-declared kind identifiers. We can also declare custom kinds:kind
is a new keyword. (In fact, there are not many variations of custom kinds. So this keyword is not very essential.)In the parameter list,
type
super parameters must be declared in front of other parameters.We can view the current general parameter declaration form
v T
as a simplified form ofv T:var
.The syntax is not the core of this proposal. Any improvement ideas are welcome.
Benefits of this proposal
type
super parameters are mainly to write some simple custom generic functions.expr
super parameters are mainly to use some functions in a clean but still performant way. For example, when using theglog
package, the clean wayis less efficient than the verbose way if the print doesn't happen finally
where
a + b + c + d
is a string concatenation expression.The current declaration of the
Info
method:By using
expr
super parameters, we can declare it asthen the above clean way will be as efficient as the verbose way for any cases.
The text was updated successfully, but these errors were encountered: