How do I create a heterogeneous Array in Scala?

前端 未结 4 566
情书的邮戳
情书的邮戳 2020-12-30 21:31

In javascript, we can do:

[\"a string\", 10, {x : 1}, function() {}].push(\"another value\");

What is the Scala equivalent?

4条回答
  •  爱一瞬间的悲伤
    2020-12-30 21:55

    Scala will choose the most specific Array element type which can hold all values, in this case it needs the most general type Any which is a supertype of every other type:

    Array("a string", 10, new { val x = 1 }, () => ()) :+ "another value"
    

    The resulting array will be of type Array[Any].

提交回复
热议问题