<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>C# 4 idea: Iterator blocks and parameter checking</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/02/c-4-idea-iterator-blocks-and-parameter-checking.aspx</link><description>Iterator blocks have an interesting property: they defer execution. When the method (or property) is called, none of your code is executed - it only starts running when MoveNext() is first called. Deferred execution is a great thing in many ways, but</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>re: C# 4 idea: Iterator blocks and parameter checking</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/02/c-4-idea-iterator-blocks-and-parameter-checking.aspx#1646312</link><pubDate>Sun, 31 Aug 2008 06:31:13 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1646312</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@al0: That&amp;#39;s fine for cases which can be easily expressed without extra logic, e.g. null argument checks - but what about more complicated cases?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1646312" width="1" height="1"&gt;</description></item><item><title>re: C# 4 idea: Iterator blocks and parameter checking</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/02/c-4-idea-iterator-blocks-and-parameter-checking.aspx#1646292</link><pubDate>Sat, 30 Aug 2008 21:35:54 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1646292</guid><dc:creator>al0</dc:creator><description>&lt;p&gt;There are enough &amp;quot;one-off&amp;quot; solutions in C# already. It would be much better to introduce a general construct that allow clearly specify contract. And it would be even better if this construct would be part of the method specification and not of the method body. I. e. it should be possible to include it into declaration of the methods in interfaces as well as into declaration of delegates.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1646292" width="1" height="1"&gt;</description></item><item><title>Daily Bits - March 6, 2008 | Alvin Ashcraft's Daily Geek Bits</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/02/c-4-idea-iterator-blocks-and-parameter-checking.aspx#1535274</link><pubDate>Thu, 06 Mar 2008 12:46:09 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1535274</guid><dc:creator>Daily Bits - March 6, 2008 | Alvin Ashcraft's Daily Geek Bits</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;Daily Bits - March 6, 2008 | Alvin Ashcraft&amp;#39;s Daily Geek Bits&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1535274" width="1" height="1"&gt;</description></item><item><title>re: C# 4 idea: Iterator blocks and parameter checking</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/02/c-4-idea-iterator-blocks-and-parameter-checking.aspx#1534077</link><pubDate>Wed, 05 Mar 2008 15:29:17 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1534077</guid><dc:creator>Ollie Riches</dc:creator><description>&lt;p&gt;It&amp;#39;s clear enough :) I get the idea of only fetching the data when it is asked for and the issue involved if it wasn&amp;#39;t deferred.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1534077" width="1" height="1"&gt;</description></item><item><title>re: C# 4 idea: Iterator blocks and parameter checking</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/02/c-4-idea-iterator-blocks-and-parameter-checking.aspx#1534021</link><pubDate>Wed, 05 Mar 2008 14:41:52 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1534021</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;Imagine they didn't defer execution, and examine a LINQ to SQL example which contains multiple method calls (Where, Select etc). If each method call had to operate immediately, you would never get the benefits of LINQ - you wouldn't get the whole query translated into SQL, you'd get one part at a time.&lt;/p&gt;
&lt;p&gt;For instance, if you do a join and then a where, you'd end up getting the whole contents of the join back from the database - far from ideal!&lt;/p&gt;
&lt;p&gt;Likewise deferred execution is necessary for streaming. Imagine if (in LINQ to Objects) Select had to execute immediately. It would have to store the data somewhere, making a copy. Because it only actually fetches data when it's asked for it, it is able to only process data as it flows through.&lt;/p&gt;
&lt;p&gt;Sorry, that's not terribly clear - but I suspect if you think about it further, you'll get it. Just imagine trying to write it in a non-deferred fashion, and then see how evil the whole thing gets.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1534021" width="1" height="1"&gt;</description></item><item><title>re: C# 4 idea: Iterator blocks and parameter checking</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/02/c-4-idea-iterator-blocks-and-parameter-checking.aspx#1534017</link><pubDate>Wed, 05 Mar 2008 14:34:33 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1534017</guid><dc:creator>Ollie Riches</dc:creator><description>&lt;p&gt;This may be a stupid question but why are iterators, LINQ queries etc deferred execution?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1534017" width="1" height="1"&gt;</description></item><item><title>re: C# 4 idea: Iterator blocks and parameter checking</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/02/c-4-idea-iterator-blocks-and-parameter-checking.aspx#1532308</link><pubDate>Mon, 03 Mar 2008 19:35:29 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1532308</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;Jared: I seem to remember there's a good reason for the second part of a &amp;quot;yield-statement-phrase&amp;quot; being a keyword. I can't think of any ways in which &amp;quot;yield validate&amp;quot; followed by a block would be valid code, but &amp;quot;yield validate;&amp;quot; certainly would be if you had a type called yield. Using a keyword as the second part prevents that situation.&lt;/p&gt;
&lt;p&gt;Fortunately, this kind of detail is one for the language designers, if they decide to go with the idea at all. (I strongly suspect it's an idea which has come up before - I'm not daft enough to think I'm likely to invent genuinely original concepts in this area.)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1532308" width="1" height="1"&gt;</description></item><item><title>re: C# 4 idea: Iterator blocks and parameter checking</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/02/c-4-idea-iterator-blocks-and-parameter-checking.aspx#1532304</link><pubDate>Mon, 03 Mar 2008 19:32:58 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1532304</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;Neil: using the Spec# approach certainly makes sense if contracts like this make it into the main body of the language. I don't think I'd want them to be introduced *just* for iterator blocks though. Of course, one problem of implementing my proposal (without Spec# contracts) is that if contracts are added later, my syntax becomes obsolete.&lt;/p&gt;
&lt;p&gt;On the other hand, it's possible that there are other things which *might* make sense to do immediately, beside parameter checking. Logging is the only one I can think of immediately...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1532304" width="1" height="1"&gt;</description></item><item><title>re: C# 4 idea: Iterator blocks and parameter checking</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/02/c-4-idea-iterator-blocks-and-parameter-checking.aspx#1532294</link><pubDate>Mon, 03 Mar 2008 19:18:14 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1532294</guid><dc:creator>Fabrice</dc:creator><description>&lt;p&gt;&amp;quot;One solution to this is to check the parameters in the public method and then call a private method which is implemented with an iterator block.&amp;quot;&lt;/p&gt;
&lt;p&gt;As a side note, this is similar to what Microsoft did when they wrote the LINQ query operators. Some operator methods check the parameters, and then delegate the processing to private classes.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1532294" width="1" height="1"&gt;</description></item><item><title>re: C# 4 idea: Iterator blocks and parameter checking</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/02/c-4-idea-iterator-blocks-and-parameter-checking.aspx#1532273</link><pubDate>Mon, 03 Mar 2008 18:52:10 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1532273</guid><dc:creator>Jared Parsons</dc:creator><description>&lt;p&gt;I really like the idea and the design. &amp;nbsp;I&amp;#39;ve hit this several times in the past and it usuually takes me a second or two to get past the idea of MoveNext() throwing an Argument*Exception. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Separating this out into a completing seperate block provides a nice clean way to do validation. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m stuck on the word &amp;quot;do&amp;quot; though because I don&amp;#39;t feel like it&amp;#39;s instructive enough. &amp;nbsp;This would be a contextual keyword so why limit it to existing keywords. &amp;nbsp;How about &amp;quot;yield validate&amp;quot;? &amp;nbsp;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1532273" width="1" height="1"&gt;</description></item><item><title>re: C# 4 idea: Iterator blocks and parameter checking</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/03/02/c-4-idea-iterator-blocks-and-parameter-checking.aspx#1531324</link><pubDate>Sun, 02 Mar 2008 23:08:47 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1531324</guid><dc:creator>Neil</dc:creator><description>&lt;p&gt;Could an approach similar to that used in Spec# be adopted with explicit detailing of preconditions for entry, together with details of the exception to throw if these preconditions are not met?&lt;/p&gt;
&lt;p&gt;E.g.&lt;/p&gt;
&lt;p&gt;public static IEnumerable&amp;lt;TResult&amp;gt; Repeat&amp;lt;TResult&amp;gt;(TResult element, int count)&lt;/p&gt;
&lt;p&gt;requires count &amp;gt; 1 otherwise ArgumentException;&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;or from (&lt;a rel="nofollow" target="_new" href="http://research.microsoft.com/specsharp/papers/Iterators.pdf"&gt;research.microsoft.com/.../Iterators.pdf&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;IEnumerable&amp;lt;int&amp;gt; FromTo(int b, int e)&lt;/p&gt;
&lt;p&gt;requires b &#x14; e + 1;&lt;/p&gt;
&lt;p&gt;invariant values.Count &#x14; e + 1 − b;&lt;/p&gt;
&lt;p&gt;invariant forall{int i in (0 : values.Count); values&lt;img src="http://msmvps.com/emoticons/emotion-55.gif" alt="Idea" /&gt; == b + i};&lt;/p&gt;
&lt;p&gt;ensures values.Count == e + 1 − b;&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;for (int x = b; x &#x14; e; x++)&lt;/p&gt;
&lt;p&gt;invariant values.Count == x − b;&lt;/p&gt;
&lt;p&gt;{ yield return x; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve always liked Spec#&amp;#39;s way of approaching these things (when I get a chance to look) and would be very interested to hear what you think.&lt;/p&gt;
&lt;p&gt;BTW - started subscribing to your blog after the excellent evening at Ascot with Iterative - thoroughly enjoyed your engaging talk and being a small part of a linq expression! Thank you.&lt;/p&gt;
&lt;p&gt;Neil&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1531324" width="1" height="1"&gt;</description></item></channel></rss>