Wednesday, December 13, 2006
Simulating Trail Argument Deduction in C#
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:
<< Home
carlo che succede? hai il blog sotto attacco? quĂ devi inventare qualcosa..e fare un post naturalmente!
Secondo me se attivi i codici di controllo con le immagini per la pubblicazione dei commenti fai cosa buona :D
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
Pazienza? :-)
<< Home





