473,320 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Inference, sloppy or good programming practice?

I was experimenting with lambda expressions this afternoon and created the
following snippet of code:

--------------------------------

delegate void Del1(byte b1);
delegate void Del2(int i1);

class FooBar
{
static void DoIt(Del1 del1)
{
}

static void DoIt(Del2 del2)
{
}

static void Main(string[] args)
{
DoIt((x) =System.Console.WriteLine(x.GetType().ToString())) ;
}
}
--------------------------------

The code above will throw the following compile error: The call is ambiguous
between the following methods or properties: 'Program.DoIt(Del1)' and
'Program.DoIt(Del2)'.

This is all very obvious and very easy to fix but the thing that I want to
point out is that if the snippet of code above only had one "DoIt()" method
then things would compile just fine the way they are. But as soon as you add
the second "DoIt()" overloaded method you get the ambiguity.

So in this example, the lack of explicitly specifying the parameter type on
the lambda expression such as:

DoIt((byte x) =System.Console.WriteLine(x.GetType().ToString())) ;
DoIt((int x) =System.Console.WriteLine(x.GetType().ToString())) ;

can cause problems in the future if the class is enhanced (such as adding
the second "DoIt" overloaded method in this case).

Now, here is the problem, supposed that the FooBar class (containing only
*one *DoIt method) was inside a dll that it's widely referenced by many
applications in your company. Also supposed that there are hundreds of
procedures that call the DoIt() method.

Now think about all the code that would break if someone added the second
overloaded DoIt() method to the FooBar class! And all of these problems
caused just because people did not fully qualify the parameter on the lambda
expression.

I realize that my example may be far-out but I am sure you get the point.

So my question to you to ponder is... Is the compiler really doing us a
favor by saving us from typing just a couple of characters?

And by the way, you can run to similar issues due to the delegate
variance/covariance feature and the compiler eagerness to try to guess what
you want to do, see snippet below:

--------------------------------

delegate void MyDel(System.IO.Stream o);

class Program
{
static void Main(string[] args)
{
MyDel d = new MyDel(DoIt);

var ms = new System.IO.MemoryStream();
d(ms);

System.Console.Read();
}

static void DoIt(object o)
{
System.Console.WriteLine("object");
}

// If this method did not exist, MyDel delegate (on
// the Main method) will bind to the DoIt() method
// with the object parameter but when this method
// is added it will bind to this method! Sometimes
// this may not be what you want!
static void DoIt(System.IO.Stream s)
{
System.Console.WriteLine("Stream");
}
}

--------------------------------

Oct 12 '08 #1
3 1061
So my question to you to ponder is... Is the compiler really doing us a
favor by saving us from typing just a couple of characters?
In the overwhelming majority of cases, yes it is - and in a number of
cases (anonymous types) we *can't* specify the argument. The scenario
you describe is a bit of a corner case, but most variants could be
addressed with generics - i.e. Action<T>. This doesn't help in the
given example, since there is nothing for it to infer the intent from.

In this corner case, perhaps the better answer is: don't introduce a
change that breaks so much code! Perhaps relocate or rename the
method... if it hurts when you move your arm so, then don't do it...

Marc

Oct 12 '08 #2
"Rene" <a@b.comwrote in message
news:uU****************@TK2MSFTNGP06.phx.gbl...
Now, here is the problem, supposed that the FooBar class (containing only
*one *DoIt method) was inside a dll that it's widely referenced by many
applications in your company. Also supposed that there are hundreds of
procedures that call the DoIt() method.
Can't you run into similar problems now anyway with a simple overload, eg

void DoSomething(object x)
{
//some code
}

then call it like this

DoSomething(null);

then someone adds this and you get an error:

DoSomething(string x)
{
//some code
}

Couldn't this sort of thing happen with all sorts of changes to a dll? (eg
removing a method). I guess you just need to take into account what effect
your changes will have on people who use your dll and make an appropriate
decision. For the number of times this would be a problem (I wouldn't be
suprised if I never encountered this in real life) I think it's a worthwhile
feature.

Michael
Oct 12 '08 #3
Rene wrote:
So my question to you to ponder is... Is the compiler really doing us a
favor by saving us from typing just a couple of characters?
There is a trade-off between overloading and type inference. In general,
type inference is more advantageous, which is why F# is so much more
concise than C#.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?u
Oct 12 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: matty | last post by:
Go away for a few days and you miss it all... A few opinions... Programming is a craft more than an art (software engineering, not black magic) and as such, is about writing code that works,...
0
by: Brett C. | last post by:
My thesis, "Localized Type Inference of Atomic Types in Python", was successfully defended today for my MS in Computer Science at the California Polytechnic State University, San Luis Obispo. With...
39
by: Noticedtrends | last post by:
Can inference search-engines narrow-down the number of often irrelevant results, by using specific keywords; for the purpose of discerning emerging social & business trends? For example, if...
12
by: G. | last post by:
Hi all, During my degree, BEng (Hons) Electronics and Communications Engineering, we did C programming every year, but I never kept it up, as I had no interest and didn't see the point. But now...
1
by: trebor | last post by:
I'm learning dotNet, although I first learned programming back in the days when structured programming was all the rage. In both my books and courses, I've seen something like this: Public Class...
150
by: tony | last post by:
If you have any PHP scripts which will not work in the current releases due to breaks in backwards compatibility then take a look at http://www.tonymarston.net/php-mysql/bc-is-everything.html and...
54
by: sam.s.kong | last post by:
Hi! I've been programming ASP for 5 years and am now learning PHP. In ASP, you can use GetRows function which returns 2 by 2 array of Recordset. Actually, it's a recommended way in ASP when you...
26
by: vlsidesign | last post by:
I am a newbie and going through "The C programming language" by Kernighan & Richie on my own time (I'm not a programmer but I want to learn because it can save me time in my normal job, and it is...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.