How do I create a heterogeneous Array in Scala? -
in javascript, can do:
["a string", 10, {x : 1}, function() {}].push("another value");
what scala equivalent?
arrays in scala homogeneous. because scala statically typed language. if need pseudo-heterogeneous features, need use immutable data structure parametrized covariantly (most immutable data structures are). list
canonical example there, vector
option. can this:
vector("a string", 10, map("x" -> 1), ()=>()) + "another value"
the result of type vector[any]
. not useful in terms of static typing, in there promised.
incidentally, "literal syntax" arrays in scala follows:
array(1, 2, 3, 4) // => array[int] containing [1, 2, 3, 4]
see also: more info on persistent vectors
Comments
Post a Comment