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.
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).
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.