Hi all,
I have this strange snippet of code:
def addAllOpt(kvs : K#typedTupleOpt[_]*) : Record[K] = {
kvs.foldLeft(this) { case (record, kv) => record add(kv._1, kv._2) }
After a lot of troubleshooting, I can’t figure out what this method means.
First of all: In the signature what meas the “sharp” symbol in “K#tupedTuple”? Does this have a particular syntax value?
Also, what means the symbol [_]* ? Why is ther a tar at the end of an empty type parameter?
And, finally, do exist a specific “add” function in the standard scala library? which take two parameter and just adds them?
Thank you all !
#
is type projection operator
[_]
is a wildcard type parameter
*
is a varargs marker
Reading that all together: the type K
has a member type called typedTupleOpt
, which is generic, so it requires a type parameter passed inside square brackets. But if the actual type parameter if not really used, it can be replаced by a wildcard _
. Finally *
after the type of the method parameter means that addAllOpt
method accepts not just one parameter of type K#typedTupleOpt[_]
but any number of them.
record add(kv._1, kv._2)
is just syntax sugar for a method call record.add(kv._1, kv._2)
where the dot was skipped
2 Likes