Re: Forevery() writable iterator mechanism in C# 3
- From: "Mike" <vimakefile@xxxxxxxxx>
- Date: Thu, 9 Mar 2006 09:59:57 -0800
"shawnk" <Shawnk@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:B5D0BAE3-20A3-4F2C-9C14-0D4C4EE189FD@xxxxxxxxxxxxxxxx
I searched the net to see if other developers have been looking for a
writable iterator in C#. I found much discussion and thus this post.
Currently (C# 2) you can not pass ref and out arguments to an iterator
method (one returning IEnumerable). I WOULD like to do this for
transformative operations on a collection.
I realize the need to lock target, etc.
Does anyone know how to handle 'writable iterators' in C# 2?
Is anyone aware of feature requests for C# to provide a forevery() with a
matching interface like IEnumerable?
Thanks ahead of time. I want to put in a product feature request but want
to
do some research before hand.
Why would the interface need ref/out itself?
There's nothing stopping the "yield"ing function from destructively
modifying it's collection regardless of what it returns, is there? (Although
it's certainly not common or expected.)
A destructive operation will by definition be refelected in all references
to the collection, so "out" is not needed.
And if it's not destructive, you can just write a little "collect" wrapper
that'll go though the enum and return a new ICollection.
IMHO, if you are:
- making a copied list (non-destructive) or returning each element, then
use a return value
- if you are performing a destructive operation, make the fn "void" and
do not return a value - this make it clear what's going on
If all your types are reference types (class instances), then each returned
element from the enumerator is a shared, mutable, object - so you can always
change it through normal means. (This isn't C++, class instances aren't
passed by value on the stack) If you want to change the "stored" element
itself of the current iteration in the underlying collection from *outside*
of the enumertor function, that's another story -- but that's really bad
practice -- especially considering that there may be no actual underlying
collection at all - that's really a major point of "yield", to provide
referential transparency in such matters. Consider:
yield return 1;
yield return 2;
yield return 3;
Your not going to be able to "write" to this interator no matter what you
do.
One way to do this would be to expose an iterator that returned proxies for
the storage of each element, if in fact there is a backing-store for the
enumeration. For example:
// Of course, generics too...
interface IEnumStorageProxy { object Value { get; set; }};
interface IEnumHasStorage { GetStorageProxyEnum(); }
foreach ( object elem in coll)
elem.Change(blah); // changing object inside collection as usual
foreach ( IEnumStorageProxy cell in coll.GetStorageProxyEnum())
{
object oldVal = c.Value;
c.Value = new MyObject(); // replace the object in the collection
}
This requires no new language features, though the interfaces would need to
be exposed from the framework collections.
m
.
- Follow-Ups:
- Re: Forevery() writable iterator mechanism in C# 3
- From: Shawnk
- Re: Forevery() writable iterator mechanism in C# 3
- From: Shawnk
- Re: Forevery() writable iterator mechanism in C# 3
- From: Mike
- Re: Forevery() writable iterator mechanism in C# 3
- Prev by Date: Re: Another form question
- Next by Date: Re: Another form question
- Previous by thread: Re: Forevery() writable iterator mechanism in C# 3
- Next by thread: Re: Forevery() writable iterator mechanism in C# 3
- Index(es):
Relevant Pages
|