You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 and c will only be evaluated if a is evaluated to true.
funcLog(abool, bstring:expr, c...interface{}:expr) {
ifa {
fmt.Printf(b, c...)
}
}
A type super parameter must be an instance of a kind. The Map, Slice, Channel, String, Numeric and Any used in the above example are some pre-declared kind identifiers. We can also declare custom kinds:
Please note that, the "lazy-evaluated" mentioned above means the operations in the said expressions are lazy evaluated, not mean the elementary values in the AST trees of the expressions.
For example, the following program prints 7 (1 + 2*3) instead of 79 (7 + 8*9).
package main
funcfoo(vint:expr, c<-chanint) int {
<-creturnv
}
funcbar(x, yint) int {
returnx*y
}
funcmain() {
vara, b, c=1, 2, 3vard=make(chainstruct{})
gofunc() {
a, b, c=7, 8, 9close(d)
}()
println(foo(a+bar(b, c), d)) // 7
}
t looks the troubles brought by expr parameters are more than their benefits.
Now, string concatenation is the only situation I am aware of which needs to be lazy evaluated (or is worth being and not impossible to be implemented). Could compiler optimizations help here? If the answer is yes, I will split this proposal into two.
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: