任何约定一个IEnumerable< T>报告不变性等特点? [英] Any convention for an IEnumerable<T> to report immutability and other characteristics?

查看:126
本文介绍了任何约定一个IEnumerable< T>报告不变性等特点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在很多情况下,code将收到的IEnumerable< T> ,并希望坚持所载物品的序列。要做到这一点的常用方法是调用了ToList 就可以了。如果有问题的实施已经是一个不可变的列表,但是,调用了ToList 上就有些浪费。

In many cases, code will receive an IEnumerable<T> and wish to persist the sequence of items contained therein. A common way to do this is to call ToList on it. If the implementation in question is already an immutable list, however, calling ToList on it would be wasteful.

如果我只路过的集合只在我的code,我可以定义使用扩展方法,这将将传递一个的IEnumerable&LT; T&GT; 的T&GT; (实现 Supercat.IImmutableList&LT;出T&GT; ),除非所提供的对象 Supercat.ImmutableList&LT的构造已经实现了 Supercat.IImmutableList&LT; T&GT; (在这种情况下它只会当做返回)。实现这样的类不会很难。我所关注的,虽然是说,如果我实现了这样一类自己,其他人谁需要这样一类也是这样,每次一个对象之间code写的谁使用不同的不可变列表班人经过,这将最终得到防守复制(到其中的previous一成不变拷贝的创建者不再承认为永恒不变的一种形式)。

If I'm were only passing collections only among my code, I could define use an extension method which would would pass an IEnumerable<T> to the constructor of Supercat.ImmutableList<T> (which implements Supercat.IImmutableList<out T>), unless the supplied object already implements Supercat.IImmutableList<T> (in which case it would simply returned as-is). Implementing such a class wouldn't be hard. My concern, though, is that if I implement such a class myself, and everybody else who needed such a class did likewise, every time an object was passed between code written by people who use different "immutable-list" classes, it would end up getting defensively copied (into a form which the previous immutable-copy's creator would no longer recognize as immutable).

所以,我很好奇:有哪些正在实现刑事质量的物体附近采取项目序列的一个不变的快照,以这样的方式,其中订阅方法的所有对象将能够识别对方的任何方法快照是不可变的名单?例如,如果很多人有自己一成不变的类包含一个函数,这将不会被用于任何其它目的,而是为了表明不变性,进而实现code有效地互操作与谁使用同样的方法将其他人的一些特定的名称可以相当容易(使用一个静态的通用类与思考)。我可以想像不少可行的办法,但我不知道任何与临界质量的物体附近。我想preFER一些公共领域的可能,从源头建立,即使这将需要使用反射,但有兴趣知道的是在那里任何办法。

Therefore, I'm curious: is there any approach which are achieving anything near criminal mass for taking an immutable snapshot of a sequence of items in such a way that all objects which subscribe to the approach will be able to recognize each other's snapshots as immutable lists? For example, if many people had their immutable classes incorporate a function with some particular name which would not be used for any other purpose but to indicate immutability, then making code efficiently interoperable with that of other people who use the same approach would be reasonably easy (using a static generic class and Reflection). I can imagine quite a few possible approaches, but I'm unaware of any with anything near critical mass. I'd prefer something public-domain which could be built from source, even if that would necessitate the use of Reflection, but would be interested in knowing of any approaches that are out there.

推荐答案

如果通过不可改变的你的意思是你不希望消费者原始数据,就能够改变的值,可以使用 ReadOnlyCollection还。然而,在这种情况下,基础列表仍可变的,所以消费者不能保证它不会被其他地方改变。

If by "immutable" you mean that you don't want consumers of the original data to be able to change the values, you can use a ReadOnlyCollection. However, in this case, the underlying list is still mutable, so consumers cannot guarantee that it won't be changed elsewhere.

如果你正在寻找真正不变的集合,你最好的选择可能是使用F#中创建的。 看到这个答案。

If you're looking for truly immutable collections, your best bet is probably to use the ones created for F#. See this answer.

这篇关于任何约定一个IEnumerable&LT; T&GT;报告不变性等特点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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