﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Bryan Watts commented on Challenge: Striving for better syntax</title><description>Or rather, the post by Eric Lippert referred by Anonymous.
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment28</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment28</guid><pubDate>Wed, 28 May 2008 20:44:13 GMT</pubDate></item><item><title>Bryan Watts commented on Challenge: Striving for better syntax</title><description>I agree. I was happy to read the post by Anonymous indicating the entire enterprise is being reworked.
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment27</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment27</guid><pubDate>Wed, 28 May 2008 20:43:40 GMT</pubDate></item><item><title>Ayende Rahien commented on Challenge: Striving for better syntax</title><description>Bryan,
  
From my perspective, this is throwing the baby with the bathwater.
  
If it it possible to make a best-effort attempt, I think that it should be done.
  
Sure, there would be pathological cases, but to block the feature because we can't handle them seems excessive to me.
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment26</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment26</guid><pubDate>Wed, 28 May 2008 19:55:13 GMT</pubDate></item><item><title>Anonymous commented on Challenge: Striving for better syntax</title><description>Further to the link that Bryan supplied, Eric Lippert just wrote a post today to follow up on this issue.
  
  
http://blogs.msdn.com/ericlippert/archive/2008/05/28/method-type-inference-changes-part-zero.aspx
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment25</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment25</guid><pubDate>Wed, 28 May 2008 18:37:16 GMT</pubDate></item><item><title>Paul Batum commented on Challenge: Striving for better syntax</title><description>Hey Jacob I like that last one, I agree even though its more verbose its nice. Maybe because it reminds me of passing parameters to a ruby method using a hash.
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment24</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment24</guid><pubDate>Wed, 28 May 2008 18:02:50 GMT</pubDate></item><item><title>Bryan Watts commented on Challenge: Striving for better syntax</title><description>Ok, so the general problem is "overload resolution with less-than-sufficient information."
  
  
Eric Lippert addressed this issue; return type inference on method groups is what gets you. He says it better than I can:
  
  
http://blogs.msdn.com/ericlippert/archive/2007/11/05/c-3-0-return-type-inference-does-not-work-on-member-groups.aspx
  
  
The final stance seems to be that the rules you are suggesting, Oren, cause confusing overload resolution rules which would violate the Principle of Least Surprise and be a source of subtle bugs.
  
  
I would be interested to hear your thoughts on that article.
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment23</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment23</guid><pubDate>Wed, 28 May 2008 17:59:07 GMT</pubDate></item><item><title>Ayende Rahien commented on Challenge: Striving for better syntax</title><description>Bryan,
  
I am try to say "when you can method A, regardless of its parameters".
  
  
I would handle this by allowing it if there are no overloads, and require disambiguation only if it is neccesary.
  
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment22</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment22</guid><pubDate>Wed, 28 May 2008 17:12:16 GMT</pubDate></item><item><title>Bryan Watts commented on Challenge: Striving for better syntax</title><description>Oren,
  
  
I guess what I'm wondering is: without resorting to arbitrary resolution, how could the problem of "method reference by name only" be different? Not just in C# land, in general.
  
  
What is the intent behind your proposed syntax? It doesn't read as anything meaningful to me. How would you state your intent in natural language?
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment21</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment21</guid><pubDate>Wed, 28 May 2008 17:05:40 GMT</pubDate></item><item><title>Jacob commented on Challenge: Striving for better syntax</title><description>Yeah; I can understand that.
  
  
This one's even more verbose (and pretty silly)--but is somehow charming:
  
  
IDuckPond pond = null;
  
pond.Stub(x =&gt; x.GetDuckById, id =&gt; default(int));
  
pond.Stub(x =&gt; x.GetDuckByNameAndQuack,
  
	name =&gt; default(string),
  
	quack =&gt; default(Quack));
  
  
Implementation:
  
  
public static void Stub&lt;T, T1&gt;(this T instance, Func&lt;T, Func&lt;T1, object&gt;&gt; fn,
  
	Func&lt;object, T1&gt; arg1) { }
  
public static void Stub&lt;T, T1, T2&gt;(this T instance, Func&lt;T, Func&lt;T1, T2, object&gt;&gt; fn,
  
	Func&lt;object, T1&gt; arg1, Func&lt;object, T2&gt; arg2) { }
  
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment20</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment20</guid><pubDate>Wed, 28 May 2008 17:00:41 GMT</pubDate></item><item><title>Ayende Rahien commented on Challenge: Striving for better syntax</title><description>Jacob,
  
That is probably the best solution, but I don't like it.
  
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment19</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment19</guid><pubDate>Wed, 28 May 2008 16:36:28 GMT</pubDate></item><item><title>Ayende Rahien commented on Challenge: Striving for better syntax</title><description>Bryan,
  
Yes, I understand what the problem is.
  
I am just not happy about it.
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment18</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment18</guid><pubDate>Wed, 28 May 2008 16:32:22 GMT</pubDate></item><item><title>Jacob commented on Challenge: Striving for better syntax</title><description>would you accept:
  
  
IDuckPond pond = null;
  
pond.Stub(x =&gt; x.GetDuckById, default(int));
  
pond.Stub(p =&gt; p.GetDuckByNameAndQuack, default(string), default(Quack));
  
  
Implemented by:
  
  
	public static void Stub&lt;T, T1&gt;(this T instance, Func&lt;T, Func&lt;T1, object&gt;&gt; fn,
  
		T1 arg1) { }
  
	public static void Stub&lt;T, T1, T2&gt;(this T instance, Func&lt;T, Func&lt;T1, T2, object&gt;&gt; fn,
  
		T1 arg1, T2 arg2) { }
  
	/* etc. */
  
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment17</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment17</guid><pubDate>Wed, 28 May 2008 16:31:59 GMT</pubDate></item><item><title>Bryan Watts commented on Challenge: Striving for better syntax</title><description>Oren,
  
  
I see what you are going for now. The code as you have written it will never be valid because it doesn't actually describe your intent.
  
  
From the compiler's perspective, you are missing information: the parameters which determines the method's signature. No sane compiler would attempt to "guess" which one you meant to invoke.
  
  
The point is that a method name by itself doesn't mean what you want it to mean. Your code is akin to walking into the house of a family and asking for a particular person by last name only. Everyone is going to look at you funny until you tell them something more.
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment16</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment16</guid><pubDate>Wed, 28 May 2008 15:42:28 GMT</pubDate></item><item><title>Ayende Rahien commented on Challenge: Striving for better syntax</title><description>Sashi,
  
There is nothing to elaborate. I want a way to get the above code to compile, that is all.
  
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment15</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment15</guid><pubDate>Wed, 28 May 2008 07:48:44 GMT</pubDate></item><item><title>Ayende Rahien commented on Challenge: Striving for better syntax</title><description>No, the purpose here is to get this to compile, because that it is the only hurdle on the way.
  
I don't want to have any external issue with the issue at hand.
  
Once you get that to compile, we can discuss how we can use this feature, but until we do, there is not point in moving on
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment14</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment14</guid><pubDate>Wed, 28 May 2008 07:37:36 GMT</pubDate></item><item><title>Sashi commented on Challenge: Striving for better syntax</title><description>Ayende - please elaborate your problem. People here are having different understandings.
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment13</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment13</guid><pubDate>Wed, 28 May 2008 06:08:03 GMT</pubDate></item><item><title>James Curran commented on Challenge: Striving for better syntax</title><description>&gt;&gt; There is not such line.  For the purpose of what I am talking about here, I don't care about actually calling this.  &lt;&lt;
  
  
So we have an implementation, without a (given) purpose.  That's kinda like says "I want to walk North, but there's a wall in the way".  The real question is "where do you want to go?" then we work on how to get there...
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment12</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment12</guid><pubDate>Wed, 28 May 2008 02:10:32 GMT</pubDate></item><item><title>Paul Batum commented on Challenge: Striving for better syntax</title><description>Another relatively obvious implementation that is very close to Krzysztof's suggestion but relies on generic parameter inference and not stub overloads:
  
  
d.Stub(x =&gt; (Func&lt;int,Duck&gt;) x.GetDuckById);
  
d.Stub(x =&gt; (Func&lt;string, Quack, Duck&gt;) x.GetDuckByNameAndQuack);
  
  
public static void Stub&lt;T, U&gt;(this T obj, Func&lt;T, U&gt; func) { }
  
  
Again, not an ideal solution I know.. just throwing it out there...
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment11</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment11</guid><pubDate>Tue, 27 May 2008 23:14:21 GMT</pubDate></item><item><title>Ayende Rahien commented on Challenge: Striving for better syntax</title><description>There is not such line.
  
For the purpose of what I am talking about here, I don't care about actually calling this.
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment10</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment10</guid><pubDate>Tue, 27 May 2008 22:52:23 GMT</pubDate></item><item><title>James Curran commented on Challenge: Striving for better syntax</title><description>My question wasn't as clear as I'd like.
  
  
Given 
  
pond.Stub( x =&gt; x.GetDuckByNameAndQuack);
  
  
presumably there will be a line which starts
  
  
Duck selectedDuck = ???????????
  
  
what's the end of that line?
  
  
  
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment9</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment9</guid><pubDate>Tue, 27 May 2008 22:48:42 GMT</pubDate></item><item><title>Ayende Rahien commented on Challenge: Striving for better syntax</title><description>I don't care if I have to do this, just as long as I don't have to do this per object that I am mocking
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment8</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment8</guid><pubDate>Tue, 27 May 2008 22:19:45 GMT</pubDate></item><item><title>Paul Batum commented on Challenge: Striving for better syntax</title><description>So I gather that we are talking about making it compile without having to put something like this alongside the IDuckPond definition?
  
  
    static class IDuckPondStub
  
    {
  
        public static void Stub(this IDuckPond pond, Func&lt;IDuckPond , Func&lt;int, Duck&gt;&gt; action) {}
  
        public static void Stub(this IDuckPond pond, Func&lt;IDuckPond, Func&lt;string, Quack, Duck&gt;&gt; action){}
  
    }
  
  
Pretty trivial to code generate, so I guess you are not interested in doing that?
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment7</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment7</guid><pubDate>Tue, 27 May 2008 22:04:38 GMT</pubDate></item><item><title>James Curran commented on Challenge: Striving for better syntax</title><description>I believe I can answer the question, if you clarify something
  
  
Given "pond.Stub( x =&gt; x.GetDuckByNameAndQuack);" compiling correctly, where would "name" and "q" come from?
  
  
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment6</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment6</guid><pubDate>Tue, 27 May 2008 20:55:09 GMT</pubDate></item><item><title>Krzysztof Koźmic commented on Challenge: Striving for better syntax</title><description>Well, for the reasons that have been mentioned the best you can do is to have:
  
  
            IDuckPond pond = null;
  
            pond.Stub&lt;IDuckPond, int, Duck&gt;(x =&gt; x.GetDuckById);
  
            pond.Stub&lt;IDuckPond, string, Quack, Duck&gt;(x =&gt; x.GetDuckByNameAndQuack);
  
  
And then lots of overloads of Stub, to look like this:
  
        public static void Stub&lt;T, T1, T2&gt;(this T item, Expression&lt;Func&lt;T, Func&lt;T1, T2&gt;&gt;&gt; expression)
  
        {
  
            //body
  
        }
  
        public static void Stub&lt;T, T1, T2, T3&gt;(this T item, Expression&lt;Func&lt;T, Func&lt;T1, T2, T3&gt;&gt;&gt; expression)
  
        {
  
            //body
  
        }
  
  
I dont think that's the way to go. With current syntax I dont really thing there's a good solution for this.
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment5</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment5</guid><pubDate>Tue, 27 May 2008 18:59:20 GMT</pubDate></item><item><title>Bryan Watts commented on Challenge: Striving for better syntax</title><description>Well there you go. I guess my Reflector class adds value because the type parameters can be inferred; you don't have to specify the return type.
  
  
But yes, the solution is the same. This would be moot if C# supported "infoof" or something to that effect. 
  
  
You might find this of interest:
  
  
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=345969&amp;SiteID=1
  
  
Matt Warren explains why "infoof" isn't already part of the language.
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment4</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment4</guid><pubDate>Tue, 27 May 2008 18:04:28 GMT</pubDate></item><item><title>Ayende Rahien commented on Challenge: Striving for better syntax</title><description>Bryan,
  
Yes, I suspected that this is the case.
  
As for your suggestion, take a look here:
  
http://www.ayende.com/Blog/archive/2005/10/29/8176.aspx
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment3</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment3</guid><pubDate>Tue, 27 May 2008 17:44:27 GMT</pubDate></item><item><title>Bryan Watts commented on Challenge: Striving for better syntax</title><description>The problem is in the overload resolution - how can the compiler determine which signature to select for a method group?
  
  
(Yes, in this case there is only 1 signature, but the general problem is "referring to a method by name").
  
  
Really, you have to give more context. The only way for the compiler to select an overload is to provide an actual invocation:
  
  
pond.Stub((string name, Quack q) =&gt; x.GetDuckByNameAndQuack(name, q));
  
  
It has been suggested that Expression&lt;&gt; can be used to do strongly-typed reflection. Perhaps something like:
  
  
public static class Reflector
  
{
  
public static MethodInfo MethodOf&lt;TResult&gt;(Expression&lt;Func&lt;TResult&gt;&gt; expression)
  
{
  
  ...Inspect expression body...
  
}
  
  
public static MethodInfo MethodOf&lt;T, TResult&gt;(Expression&lt;Func&lt;T, TResult&gt;&gt; expression)
  
{
  
  ...Inspect expression body...
  
}
  
  
public static MethodInfo MethodOf&lt;T1, T2, TResult&gt;(Expression&lt;Func&lt;T1, T2, TResult&gt;&gt; expression)
  
{
  
  ...Inspect expression body...
  
}
  
  
...And so on for other Func&lt;&gt; types...
  
  
...MemberOf, PropertyOf, and FieldOf would also be useful...
  
}
  
  
used like:
  
  
pond.Stub(Reflector.MethodOf(
  
  (string name, Quack q) =&gt; x.GetDuckByNameAndQuack(name, q)));
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment2</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment2</guid><pubDate>Tue, 27 May 2008 17:36:50 GMT</pubDate></item><item><title>Steve commented on Challenge: Striving for better syntax</title><description>Not possible unfortunately, you can make it work for the first scenario with a single argument but it will not work in a general case
  
  
Ideas?  BooLangStudio ; )
  
  
</description><link>http://ayende.com/3337/challenge-striving-for-better-syntax#comment1</link><guid>http://ayende.com/3337/challenge-striving-for-better-syntax#comment1</guid><pubDate>Tue, 27 May 2008 16:51:20 GMT</pubDate></item></channel></rss>