In javascript, we can do:
['a string', 10, {x : 1}, function() {}].push('another value');
What is the Scala equivalent?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Arrays in Scala are very much homogeneous. This is because Scala is a statically typed language. If you really need pseudo-heterogeneous features, you need to use an immutable data structure that is parametrized covariantly (most immutable data structures are).
Listis the canonical example there, butVectoris also an option. Then you can do something like this:The result will be of type
Vector[Any]. Not very useful in terms of static typing, but everything will be in there as promised.Incidentally, the ‘literal syntax’ for arrays in Scala is as follows:
See also: More info on persistent vectors