IanG on Tap

Ian Griffiths in Weblog Form (RSS 2.0)

Blog Navigation

April (2018)

(1 item)

August (2014)

(1 item)

July (2014)

(5 items)

April (2014)

(1 item)

March (2014)

(1 item)

January (2014)

(2 items)

November (2013)

(2 items)

July (2013)

(4 items)

April (2013)

(1 item)

February (2013)

(6 items)

September (2011)

(2 items)

November (2010)

(4 items)

September (2010)

(1 item)

August (2010)

(4 items)

July (2010)

(2 items)

September (2009)

(1 item)

June (2009)

(1 item)

April (2009)

(1 item)

November (2008)

(1 item)

October (2008)

(1 item)

September (2008)

(1 item)

July (2008)

(1 item)

June (2008)

(1 item)

May (2008)

(2 items)

April (2008)

(2 items)

March (2008)

(5 items)

January (2008)

(3 items)

December (2007)

(1 item)

November (2007)

(1 item)

October (2007)

(1 item)

September (2007)

(3 items)

August (2007)

(1 item)

July (2007)

(1 item)

June (2007)

(2 items)

May (2007)

(8 items)

April (2007)

(2 items)

March (2007)

(7 items)

February (2007)

(2 items)

January (2007)

(2 items)

November (2006)

(1 item)

October (2006)

(2 items)

September (2006)

(1 item)

June (2006)

(2 items)

May (2006)

(4 items)

April (2006)

(1 item)

March (2006)

(5 items)

January (2006)

(1 item)

December (2005)

(3 items)

November (2005)

(2 items)

October (2005)

(2 items)

September (2005)

(8 items)

August (2005)

(7 items)

June (2005)

(3 items)

May (2005)

(7 items)

April (2005)

(6 items)

March (2005)

(1 item)

February (2005)

(2 items)

January (2005)

(5 items)

December (2004)

(5 items)

November (2004)

(7 items)

October (2004)

(3 items)

September (2004)

(7 items)

August (2004)

(16 items)

July (2004)

(10 items)

June (2004)

(27 items)

May (2004)

(15 items)

April (2004)

(15 items)

March (2004)

(13 items)

February (2004)

(16 items)

January (2004)

(15 items)

Blog Home

RSS 2.0

Writing

Programming C# 5.0

Programming WPF

Other Sites

Interact Software

Jon Skeet on Generics

Friday 30 September, 2005, 08:00 PM

I just tried posting this as a comment to Jon Skeet's blog, but the the CAPTCHA on MSMVPs seems to be completely broken... Either that, or it's right and I'm really not human. Since I was already planning to post about the fact that Jon is now blogging, I thought I might as well just post my comment here anyway. Jon already has a great C# resources page, and I'm expecting his blog to be well worth reading, and his first few posts don't disappoint. What follows is a reply to his "Nasty generics restriction" article.

In essence, Jon is really saying he'd rather be using ML. :) Here's the ML version of his doItTwice function:

fun doItTwice x y = x (x (y));

Here's the static type inferred by the ML compiler when given that function definition:

val doItTwice = fn : ('a -> 'a) -> 'a -> 'a

That is to say, doItTwice is a function whose first parameter is a function whose type is 'a -> 'a. (I.e. a function whose return type is the same as its parameter type. And that's all we know - we don't know what the actual type being passed in at this stage is. This is polymorphic code, so it doesn't care what the specific parameter/return type is.) doItTwice's second parameter must be the same type as whatever the function passed in expects as a parameter. doItTwice will return a value of that self same type.

(It would be interesting to see what F# does with this. F# is a .NET language that lets you do this kind of thing. My guess is that what happens in F# stays in F#, so it doesn't have to solve the problem of how to map this into the world of .NET and delegates. I've not got around to installing F# yet, so I could be wrong.)

Of course you can write such a method in C# with generics:

public static T DoItTwice<T> (Func<T,T> func, T input)
{
  return func(func(input));
}

And we can even define a generic delegate type that can refer to such a thing:

public delegate T Applier<T>(Func<T, T> func, T input);

and we can initialize variables of this type:

Applier<int> appInt = DoItTwice<int>;
Applier<string> appString = DoItTwice<string>;

But I think the thing Jon wants is to have a delegate instance refer to this generic method without committing to the generic type parameters. Something like this:

Applier<T> appT = DoItTwice<T>;

or whatever syntax you deem fit...

Which brings me to my question for Jon - moving out of the realm of lambda syntax for a moment, what would you expect a variable declaration for one of these things to look like? I don't think my example above is right, because that syntax already means something. At least it does if I've got some type called T already... I don't know what syntax Jon will ask for, but I suppose it could look like this:

Applier<T> appT<T> = DoItTwice<T>;

Note the generic syntax applied to the variable name itself.

This implies the introduction of variables and/or fields that can hold a reference to any instance of a generic. This is subtly different from what we can do with .NET generics today. I'm not sure if Jon's asking for this, or just a solution for lambdas and/or delegates. Or something else...

Copyright © 2002-2024, Interact Software Ltd. Content by Ian Griffiths. Please direct all Web site inquiries to webmaster@interact-sw.co.uk