"阵列与QUOT;在斯卡拉 [英] "Arrays" in Scala

查看:132
本文介绍了"阵列与QUOT;在斯卡拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,我们可以这样做:

In javascript, we can do:

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

什么是斯卡拉相当于?

What is the Scala equivalent?

推荐答案

在Scala的数组是非常均匀。这是因为Scala是一种静态类型语言。如果你真的需要伪异构特性,你需要使用的协变参数化(大部分不可变的数据结构)的不可变的数据结构。 列表是典型的例子有,但<一href=\"http://www.$c$ccommit.com/blog/misc/implementing-persistent-vectors-in-scala/final/Vector.scala\"><$c$c>Vector也是一种选择。然后,你可以做这样的事情:

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). List is the canonical example there, but Vector is also an option. Then you can do something like this:

Vector("a string", 10, Map("x" -> 1), ()=>()) + "another value"

结果将是类型矢量[任意] 。在静态类型方面不是很有用,但一切都会在那里的承诺。

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:

Array(1, 2, 3, 4)     // => Array[Int] containing [1, 2, 3, 4]

另请参见更多信息上持续载体

这篇关于&QUOT;阵列与QUOT;在斯卡拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆