Marshal wrote:
> Sure... IEnumerable was inconvenient suggesting a separate class to service
> the enumeration, IEnumerator, and multiple operations: Current, MoveNext,
> Reset. (I'll warp the definition of "operation" for a second if you don't
> mind).
IEnumerable expresses that enumeration is supported, "I am iterable, you
can obtain that enumeration by invoking GetEnumerator()".
IEnumerator expresses a "pointer" into an enumeration, "This is how far
you are along the enumeration".
IEnumerator have Current/MoveNext(), JAVA Iterator have hasNext() and
next(), C++ iterators have "++" and "*". Actually I don't recall seeing
any protocol for iteration that don't have seperate
advance/advance-check and value obtain, and thus multiple operations.
I would say that whether Reset() should be included in IEnumerator is
open to discussion.
> So, it essentially redefines the concepts of how functions are supposed to
> behave, as well as how return values interact with return types. It also
> introduces the new keyword with a very specialized purpose.
It lets you define a generator for IEnumerator using function-syntax.
Other languages have this too, python and it is essential in ruby.
Whether this "redefines the concepts of how functions work" or not...
well, you are really just defining a function that returns an
enumerator. There is no "quantum magic" here, only compiler assisted
closure greneration.
Notice that the caller of the function doesn't care if actual "quantum
magic" was used to implement the IEnumerator returned. The implementer u
could do it by implementing the closure, MoveNext() and Current by hand
in a class that stores the relevant state and implement IEnumerator.
Function-syntax is rather nice for describing enumeration, and
syntactic-sugar support for storing the used local-variables in a
helper-class and implementing Current/MoveNext() is convinient to me in
that syntax.
> One way to explain "yield", is to say that it boxes your object into an
> IEnumerator, saves the execution state of your function, and also restores
> the execution state the next time your function is called, even before the
> keyword is invoked during regular instruction flow, so it establishes the
> loop's next startup state. Therefore, it redefines the laws of instruction
> sequencing, and is sort of a quantum keyword that exists in multiple spots at
> once during the existance of that function.
Another way to explain it would be that it generates an IEnumerators
MoveNext() and Current functions from the closure available in the
function. This is a known technique known from functional programming: a
higher-order function.
> It would make sense to have IEnumerate be an allowed combination of
> IEnumerable and IEnumerator, and to add a GetNext function that can return
> the proper generic variable type. Then make "yield" its own return-style
> keyword:
I don't see the benefit of combining IEnumerable and IEnumerator, see above.
The problems in combining the two concepts is manifest in our
example-code. Where is _collection coming from (the interface-usage
prevents you from having a protocol of accepting it at construction time).
> (** Proposed Syntax:
> public class Woo : IEnumerate
> {
> ...
> public object GetNext() {
> if(_collection==null) return null;
> foreach(object obj in _collection) {
> yield obj;
> }
> }
> ...
> }
> **)
I don't really understand how this is substantially different from or
better than:
public static IEnumerator Enumerate(ICollection c) {
foreach ( object o in c )
yield return o;
}
Which fits into the current enumeration-idiom in C#.
The choice of "yield return" vs. just "yield" is open to discussion, but
i suspect "yield return" was chosen because no program containing that
sequence of tokens would be valid in previous C# revisions.
> This would still allow the full existing enumeration capability, but would
> introduce this new style in a cleaner and more intuitive way.
Perhaps you are finding the IEnumerable/IEnumerator design
non-intuitive, and perhaps transfer that onto the "yield return" construct?
I find that the IEnumerable/IEnumerator design is spot-on and I haven't
really seen any languages which doesn't make that distinction in one
form or another.
Perhaps you need just a little more assimilation before you accept the
design

(notice smiley.... i'm not being hostile here)
--
Helge Jensen
private.php?do=newpm&u=
sip:(E-Mail Removed)
-=> Sebastian cover-music:
http://ungdomshus.nu <=-