什么是异构阵列的地步? [英] what is the point of heterogenous arrays?

查看:182
本文介绍了什么是异构阵列的地步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,更动态比Java语言,如Python和Ruby,常常让你把混合类型的对象数组,像这样:

I know that more-dynamic-than-Java languages, like Python and Ruby, often allow you to place objects of mixed types in arrays, like so:

["hello", 120, ["world"]]

我不明白的是,为什么你会永远使用这样的功能。如果我想保存在Java中异构数据,我通常会为它创建一个对象。

What I don't understand is why you would ever use a feature like this. If I want to store heterogenous data in Java, I'll usually create an object for it.

举例来说,假设一个用户 INT ID 字符串名称。当我看到在Python / Ruby的/ PHP,你可以做这样的事情:

For example, say a User has int ID and String name. While I see that in Python/Ruby/PHP you could do something like this:

[["John Smith", 000], ["Smith John", 001], ...]

这似乎有点不太安全/ OO不是创建一个类用户带属性的 ID 名称,然后让你的数组:

this seems a bit less safe/OO than creating a class User with attributes ID and name and then having your array:

[<User: name="John Smith", id=000>, <User: name="Smith John", id=001>, ...]

其中的&lt;使用者...方式&gt; 的事情再present用户对象

where those <User ...> things represent User objects.

有没有理由使用前者优于后者支持它的语言吗?或者是有一些更大的原因使用异构阵列?

Is there reason to use the former over the latter in languages that support it? Or is there some bigger reason to use heterogenous arrays?

N.B。我不是在谈论阵列,包括所有实现相同的接口或同一母公司的,例如继承不同的对象:

N.B. I am not talking about arrays that include different objects that all implement the same interface or inherit from the same parent, e.g.:

class Square extends Shape
class Triangle extends Shape
[new Square(), new Triangle()]

,因为这是,对程序员至少,仍然是一个同质数组,你会与每个形状做同样的事情(比如,调用画()法)中,只有这些方法通常在两者之间定义

because that is, to the programmer at least, still a homogenous array as you'll be doing the same thing with each shape (e.g., calling the draw() method), only the methods commonly defined between the two.

推荐答案

由于katrielalex写道:有什么理由不支持异构名单。事实上,不允许它需要静态类型,我们又回到了老的争论。但是让我们这样做避免,而是回答为什么你会使用部分...

As katrielalex wrote: There is no reason not to support heterogeneous lists. In fact, disallowing it would require static typing, and we're back to that old debate. But let's refrain from doing so and instead answer the "why would you use that" part...

说实话,它不使用的的太多 - 如果我们使用异常的在你的最后一段,并选择更自由的定义实现相同的接口,比如Java或C#。几乎所有我的迭代捣弄code的期望所有项目实施一些接口。当然它确实,otheriwise它可能很少做吧!

To be honest, it is not used that much -- if we make use of the exception in your last paragraph and choose a more liberal definition of "implement the same interface" than e.g. Java or C#. Nearly all of my iterable-crunching code expects all items to implement some interface. Of course it does, otheriwise it could do very little to it!

不要误会我的意思,也有绝对有效的使用案例 - 有很少一个很好的理由写一整类包含一些数据(即使你添加一些可调用,函数式编程有时就派上用场了)。一个字典将是一个比较常见的选择,虽然, namedtuple 很整齐也。但他们比你好像觉得不太常见,他们有思想和纪律,而不是为牛仔编码。

Don't get me wrong, there are absolutely valid use cases - there's rarely a good reason to write a whole class for containing some data (and even if you add some callables, functional programming sometimes comes to the rescue). A dict would be a more common choice though, and namedtuple is very neat as well. But they are less common than you seem to think, and they are used with thought and discipline, not for cowboy coding.

(另外,你用户嵌套表的例子不是一个很好的 - 由于内部列表是固定大小的,你最好使用元组,这使得它即使是有效的哈斯克尔(类型是 [(字符串,整数)] ))

(Also, you "User as nested list" example is not a good one - since the inner lists are fixed-sized, you better use tuples and that makes it valid even in Haskell (type would be [(String, Integer)]))

这篇关于什么是异构阵列的地步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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