Varargs with different type parameters in scala -
i'm new scala...
anyway, want like:
val bar = new foo("a" -> list[int](1), "b" -> list[string]("2"), ...) bar("a") // gives list[int] containing 1 bar("b") // gives list[string] containing "2"
the problem when do:
class foo(pairs: (string, list[_])*) { def apply(name: string): list[_] = pairs.tomap(name) }
pairs
gonna array[(string, list[any]) (or that) , apply()
wrong anyway since list[_]
1 type instead of "different types". if varargs * returned tuple i'm still not sure how i'd go getting bar("a")
return list[originaltypepassedin]
. there way of doing this? scala seems pretty flexible feels there should advanced way of doing this.
this can't done. consider this:
val key = readstringfromuser(); val value = bar(key);
what type of value
? depend on user has input. types static, they're determined , used @ compile time.
so you'll either have use fixed number of arguments know types @ compile time, or use generic vararg , type casts during runtime.
Comments
Post a Comment