<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.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>I Dream Of Whidbey: Generics and Anonymous Methods</title><link>http://blogs.msdn.com/chrisgarty/archive/2004/03/16/89729.aspx</link><description>We (the team at iP3) are implementing an object model using Whidbey at the moment, and we have been copying and pasting a lot of code lately which is one of the things that I try to minimise as much as possible. One method that we have copied a lot is</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: I Dream Of Whidbey: Generics and Anonymous Methods</title><link>http://blogs.msdn.com/chrisgarty/archive/2004/03/16/89729.aspx#90047</link><pubDate>Tue, 16 Mar 2004 16:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:90047</guid><dc:creator>Dominic Cooney</dc:creator><description>Why do you need to use delegates? All of the objects your delegates return already exist and the delegates always get called anyway, so it is not lazy creation! Is it to limit what RelationshipHelper can do with the collections? Probably not, because any client who does ClassA::Contains can mutate the array (insert nulls, insert duplicate clients, etc.) Are you aiming for CLS compliance? Probably not-- RelationshipHelper exposes generic types. I think a much simpler and better approach would be to make _contains an IList&amp;lt;ClassB&amp;gt;; burn a few words of memory and create an empty list in the constructor instead of polluting your code with if _contains != null; return a defensive copy in ClassA::Contains (see Effective Java p. 122) to protect your invariants; and go for the obvious code in ClassA::AddClassB1. There are some other dubious things here: Is being able to contain nulls by design? If not, ClassA::AddClassB? should throw a NullReferenceException. And since ClassB should only appear in one ClassA, I would either make ClassB::ContainedIn into a set-once property, or have it remove itself from its previous container. Just my $0.02.</description></item><item><title>re: I Dream Of Whidbey: Generics and Anonymous Methods</title><link>http://blogs.msdn.com/chrisgarty/archive/2004/03/16/89729.aspx#90084</link><pubDate>Tue, 16 Mar 2004 17:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:90084</guid><dc:creator>Chris Garty</dc:creator><description>Thanks for your comments Dominic.&lt;br&gt;The use of delegates was dreamed up since there are a number of classes that have to implement this relationship. &lt;br&gt;We had thought that maybe we could increase code reuse if we used anonymous methods i.e. we wouldn't have to copy and paste the &amp;quot;copy member array to list, add instance to list, and copy list to member array&amp;quot; code since we could have that sit somewhere else and just create delegates for the SetSingle, SetArray, GetArray parts.&lt;br&gt;Making _contains an IList&amp;lt;ClassB&amp;gt; was something we had talked about, but I wanted to do this as a prototype first to see if it was viable.&lt;br&gt;I hadn't thought about the containing of nulls. A good point, I'll look out for this in the future.&lt;br&gt;Thanks&lt;br&gt;&lt;br&gt;- Chris</description></item><item><title>re: I Dream Of Whidbey: Generics and Anonymous Methods</title><link>http://blogs.msdn.com/chrisgarty/archive/2004/03/16/89729.aspx#90200</link><pubDate>Tue, 16 Mar 2004 21:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:90200</guid><dc:creator>Sean Malloy</dc:creator><description>Chris,&lt;br&gt;&lt;br&gt;something else that excites me about Generics and Anonymous Methods:&lt;br&gt;&lt;br&gt;The ability to do Smalltalk like select/detect/collect methods on the Collection classes (Ofcourse, its not smalltalk, so you can't directly modify the library classes).&lt;br&gt;&lt;br&gt;How about this:&lt;br&gt;&lt;br&gt;public delegate bool IsTrueBlock(T obj);&lt;br&gt;&lt;br&gt;public class SmalltalkList&amp;lt;T&amp;gt; : ArrayList&amp;lt;T&amp;gt;&lt;br&gt;{&lt;br&gt;	public IList&amp;lt;T&amp;gt; Select(IsTrueBlock block)&lt;br&gt;	{&lt;br&gt;		IList&amp;lt;T&amp;gt; list = new ArrayList&amp;lt;T&amp;gt;();&lt;br&gt;		foreach (&amp;lt;T&amp;gt; o in this)&lt;br&gt;		{&lt;br&gt;			if (block(o))&lt;br&gt;				list.Add(o);&lt;br&gt;		}&lt;br&gt;		return list;&lt;br&gt;		&lt;br&gt;	}&lt;br&gt;&lt;br&gt;	public &amp;lt;T&amp;gt; Detect(IsTrueBlock block)&lt;br&gt;	{&lt;br&gt;		foreach (&amp;lt;T&amp;gt; o in this)&lt;br&gt;		{&lt;br&gt;			if (block(o))&lt;br&gt;				return o;&lt;br&gt;		}&lt;br&gt;		return null;&lt;br&gt;	}&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;SmalltalkList&amp;lt;Person&amp;gt; list = new SmalltalkList&amp;lt;Person&amp;gt;();&lt;br&gt;list.Add(new Person(&amp;quot;John&amp;quot;, 22));&lt;br&gt;list.Add(new Person(&amp;quot;Chris&amp;quot;, 25));&lt;br&gt;list.Add(new Person(&amp;quot;Sean&amp;quot;, 23));&lt;br&gt;list.Add(new Person(&amp;quot;Amy&amp;quot;, 22));&lt;br&gt;list.Add(new Person(&amp;quot;Frank&amp;quot;, 29));&lt;br&gt;list.Add(new Person(&amp;quot;Adrian&amp;quot;, 41));&lt;br&gt;&lt;br&gt;IList&amp;lt;Person&amp;gt; overForty = list.Select(delegate(Person p) { return p.Age &amp;gt; 40; });&lt;br&gt;// should only have Adrian in the list&lt;br&gt;&lt;br&gt;Person ageEquals = list.Detect(delegate(Person p) { return p.Age == 22; });&lt;br&gt;// should be John (Detect returns first instance that matches)&lt;br&gt;&lt;br&gt;&lt;br&gt;I don't even know if what I have written will compile (I have no access to whidbey). I'm simply theorizing. But if it does work, writing generic selection methods on collections will be quite nice. &lt;br&gt;&lt;br&gt;It makes me want a language syntax for lambda functions rather than the current state.. ie:&lt;br&gt;&lt;br&gt;rather than&lt;br&gt;&lt;br&gt;Select(delegate(Person p) { return p.Age &amp;gt; 10; });&lt;br&gt;&lt;br&gt;Have somehting like&lt;br&gt;&lt;br&gt;Select([ Person p | return p.Age &amp;gt; 10; ]); // or is too close to smalltalk! ;))&lt;br&gt;&lt;br&gt;I don't see why the C# Compiler couldn't turn that syntax into MSIL.</description></item><item><title>Take Outs for 15 March 2004</title><link>http://blogs.msdn.com/chrisgarty/archive/2004/03/16/89729.aspx#90225</link><pubDate>Tue, 16 Mar 2004 06:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:90225</guid><dc:creator>Enjoy Every Sandwich</dc:creator><description>You have been Taken Out! Thanks for the post.</description></item><item><title>re: I Dream Of Whidbey: Generics and Anonymous Methods</title><link>http://blogs.msdn.com/chrisgarty/archive/2004/03/16/89729.aspx#90759</link><pubDate>Wed, 17 Mar 2004 16:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:90759</guid><dc:creator>Dominic Cooney</dc:creator><description>Re: Smalltalk and lambda expressions: One difficulty is that delegates have name, not structural, equality. Therefore delegate int A(int x) and delegate int B(int x) are different, incompatible types. This decision makes sense if you think delegates have an implied 'contract'. On the other hand, lambda expressions are really cool and useful. I think to have any hope of getting this in the CLR someone should start to incrementally modify Rotor: Firstly, to modify the CLI to implement a subtyping rule for delegates, which is the moral equivalent of having a first-class function type; then to modify C#.</description></item><item><title>re: I Dream Of Whidbey: Generics and Anonymous Methods</title><link>http://blogs.msdn.com/chrisgarty/archive/2004/03/16/89729.aspx#90884</link><pubDate>Wed, 17 Mar 2004 20:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:90884</guid><dc:creator>Sean Malloy</dc:creator><description>Dominic,&lt;br&gt;&lt;br&gt;Yes, its also why you can't create two delegates with the same name.&lt;br&gt;&lt;br&gt;However, in the context that the delegate is used, it still has a declared Type. And the type is the name of the delegate.&lt;br&gt;&lt;br&gt;IE:&lt;br&gt;&lt;br&gt;public delegate bool IsTrue(Person p);&lt;br&gt;&lt;br&gt;Correct me if I'm wrong, but this should be theoretically (with langauge extension):&lt;br&gt;&lt;br&gt;IsTrue block = [ Person p | return p.Age &amp;gt; 10; ];&lt;br&gt;&lt;br&gt;you can ascertain the ecact nature of the lambda base don its declared type vs the interface contract yes?&lt;br&gt;&lt;br&gt;Same as per a method declaration:&lt;br&gt;&lt;br&gt;public void Select(IsTrue block) { ... }&lt;br&gt;&lt;br&gt;...&lt;br&gt;&lt;br&gt;list.Select([ Person p | return p.Age &amp;gt; 10; ]);&lt;br&gt;&lt;br&gt;the contract implies that the piece of code being passed in, is a delegate which matches the contract specified by the delegate definition of: public delegate bool IsTrue(Person p);&lt;br&gt;&lt;br&gt;&lt;br&gt;you say:&lt;br&gt;&lt;br&gt;&amp;quot;delegate int A(int x) and delegate int B(int x) are different, incompatible types&amp;quot;&lt;br&gt;&lt;br&gt;show me the whidbey code where you could pass in one delegate of type A, to a method that is expecting a delegate of type B:&lt;br&gt;&lt;br&gt;A methodA = delegate(int x) { return x; };&lt;br&gt;B methodB = delegate(int x) { return x; };&lt;br&gt;&lt;br&gt;void Select(B method) { }&lt;br&gt;&lt;br&gt;would throw a compilation error if you tried to pass an A delegate to it.&lt;br&gt;&lt;br&gt;Now look at the following code:&lt;br&gt;&lt;br&gt;A methodA = [int x | return x; ];&lt;br&gt;B methodB = [int x | return x; ];&lt;br&gt;&lt;br&gt;sure, they look the same, but they ARE different types. &lt;br&gt;&lt;br&gt;So I'm only arguing for a syntactic change. The rest of the support for what I want is lready there in whidbey, its merely the way a an anonymous delegate/method is declared in C# is what I'm asking for</description></item><item><title>re: I Dream Of Whidbey: Generics and Anonymous Methods</title><link>http://blogs.msdn.com/chrisgarty/archive/2004/03/16/89729.aspx#90895</link><pubDate>Wed, 17 Mar 2004 20:53:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:90895</guid><dc:creator>Sean Malloy</dc:creator><description>I should have said&lt;br&gt;&lt;br&gt;&amp;quot;Yes, its also why you can't create two delegates with the same name but different parameters&amp;quot;.</description></item></channel></rss>