« C# Behind the Scenes | Main | Nested Blocks »

August 05, 2004

Comments

Sean Hernandez

Great blog entry. Also, it is weird that I am currently writing a general purpose m-stage pipeline with the same behavior.

David Brabant

Very nice article. Thanks. I was planning to start something along that line myself but didn't find the time until now. About continuations, there is quite a number of interesting articles on Christian Queinnec's pages (and of course, but you probably know that, the chapters in "Structure and Interpretation of Computer Programs" by Abelson and Sussman). Christian has written some nice articles showing how continuations can be useful in a web context. And finally, googling for "Luc Moreau" + scheme and continuations should also bring you a lot of interesting material.

vbat

nice entry, thanks.
Actually, IIUC iterators in c# are semicoroutine in that tranfer of data is allowed just in one sense (i.e. you have a yield on one side and not on the other. )

About continuation the main difference is that I don't think you /can/ simulate conts with coroutines, cause the formers are more general than the latter.
It's like simulating an if with a while :)

See http://www.c2.com/cgi/wiki?CoRoutine

Dan Golick

Hmmm... I was just reading a posting where someone was looking for a tokenizer in the .net framework rather than split.

It seems that an iterator would make a great tokenizer with the abilty to return each token and retain state to find the next token.

James Bellinger

Slightly off topic... Any idea why the C# folks decided to go with 'yield return' instead of just 'yield' for the usual call? It seems so ugly, so I'd be curious of the rationale behind it.

Daniel O'Connell

It allows yield to be a context sensitive keyword(I think yield return is a spaced keyword, but the effects are the same), basically. By using yield return the compiler doesn't have to add any new keywords, so these lines
int yield;
yield crop;
yield+=10;
crop.Whatever()
are still legal because yield isn't immediatly followed by return. None of the keywords introduced in C# 2.0 are breaking. global has to be postfixed with ::, alias only works in the using directive section, where only works in the constraints section, partial is only valid as a type modifier, etc.

Jason Whittington

The syntax was just "yield" in earlier builds. However the change to "yield return" also made possible "yield break", which is handy because it stops iteration (MoveNext returns false). This lets you bail out of iteration if you decide you have nothing left to give. For example, the function below will only return 1 (and in fact the compiler will complain that unreachable code is detected).

IEnumerableint Sequence()
{
yield return 1; //return 1
yield break; //abort! (return false from MoveNext)
yield return 2; //never executes

}

Keith J. Farmer

Actually, according to Joval Lowy, "yield" conflicted with existing variable names in financial applications.

Christopher

Yield was a reserved word in C# 1.0. Mr. Whittington's statement is correct.

The comments to this entry are closed.

My name is Wesner Moise. I am a software entrepreneur developing revolutionary AI desktop applications. I worked as a software engineer in Microsoft Excel group for six years during the 1990s. I worked on PivotTables and wrote the most lines of code in Excel 97-- about 10 times the median developer. I have a Harvard BA in applied math/computer science and a UCLA MBA in technology entrepreneurship. I am a member of the Triple Nine Society, a 99.9 percentile high-IQ society.

December 2013

Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31