D vs. C#

Julio César Carrascal Urquijo jcesar at phreaker.net
Fri Nov 24 11:53:36 PST 2006


Marcio wrote:
> Julio César Carrascal Urquijo wrote:
>> C# things that don't have an equivalent in D.
>> * yield operator
> 
> 
>     I'd like to point out that the Concurrency and Coordination Runtime 
> (CCR) makes heavy use of yield.

Didn't know that. Thanks for the info. The yield operator in C# it's 
another time-saver and I probably should have written more about it.

One of the coolest thing I wrote with yield was an encapsulation of 
several loops that iterated over the fields of an object using 
reflection. All of them needed some start, finish and filtering 
conditions but the code was very similar.

I started writing a class that implemented IEnumerator and splitting the 
loop to implement the interface was starting to get very hard. Then I 
remembered yield and the IEnumerable interface and everything got 
converted to a single method and a couple of properties. Something like:

class FieldInfoEnumerable
	: System.Collections.Generic.IEnumerable<FieldInfo>
{
	public IEnumerator<FieldInfo> GetEnumerator()
	{
		Type type = m_initialType;
		do
		{
			foreach (FieldInfo fi in type.GetFields(m_bindingFlags))
				yield return fi;
			type = type.BaseType;
		}
		while (type != m_finalType);
	}
}


The compiler made the hard work and converted the loop into a 
IEnumerator for me. I wish D had a yield operator. Maybe for D 2.0.

I'm reading the OOPSLA pdf and downloading the videos right now.

Thanks again.



More information about the Digitalmars-d mailing list