Wednesday, December 13, 2006

 

Simulating Trail Argument Deduction in C#

I've been in Lugano for a couple of days, teaching C# and .NET. The guys there are quite creative (possibly too much :-), and I've got quite a few interesting questions.
Somehow, they must have inspired me :-), because although we didn't talk about generics yet, on the way home I realized there is an extremely simple way to simulate trail arguments deduction in C# (see an old post of mine about C# lacking that feature).
Let's review a trivial example:
class Test
{
public static void f< T1, T2 >(T2 p)
{
// would use T1 and T2 in real cases
}
}
class App
{
public static void Main()
{
Test.f< double, int >(3); // works
// Test.f< double >(3); // error
}
}


If we accept a small twist in syntax, we can easily simulate trail argument deduction by adding a class:
class Test
{
public static void f< T1, T2 >(T2 p)
{
// would use T1 and T2 in real cases
}
}
class Test< T1 >
{
public static void f< T2 >(T2 p)
{
Test.f< T1, T2 >(p);
}
}
class App
{
public static void Main()
{
Test.f< double, int >(3); // works
// Test.f< double >(3); // error
Test< double >.f(3); // works
}
}


Not exactly the same thing, but good enough in many practical cases.

Comments:
This post has been removed by a blog administrator.
 
This post has been removed by a blog administrator.
 
carlo che succede? hai il blog sotto attacco? quĂ  devi inventare qualcosa..e fare un post naturalmente!
 
This post has been removed by a blog administrator.
 
Secondo me se attivi i codici di controllo con le immagini per la pubblicazione dei commenti fai cosa buona :D
 
Si' ma aspettate un attimo che devo ancora visitare tutti i siti... carini!
 
Alla fine ho attivato la word verification, ho sempre pensato che fosse un fastidio eccessivo per chi vuole commentare, ma lo spam stava diventando davvero troppo (devo ancora ripulire qualche vecchio messaggio).
Pazienza? :-)
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?