Generic parameter deduction

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi all,

I have a method where the compiler is unable to deduce the generic arguments. Now I'm asking myself if this a bug, or if not, why it isn't working. The method in question is Foo3:

using System;
using System.Collections.Generic;

namespace ConsoleApplication10
{
    class Program
    {
        class A<T> { }
        class B<T> { }

        static A<T> Foo1<T, AA>(AA a, T t) where AA : A<T>
        {
            return default(A<T>);
        }
        static A<T> Foo2<T, AA>(AA a, B<T> t) where AA : A<T>
        {
            return default(A<T>);
        }
        static A<T> Foo3<T, AA>(AA a) where AA : A<T>
        {
            return default(A<T>);
        }
        static void Main(string[] args)
        {
            A<int> ai = new A<int>();

            ai = Foo1(ai, 42);
            ai = Foo2(ai, new B<int>());

            // ... gives CS0411, can't deduce generic parameter. Why?
            ai = Foo3(ai);
        }
    }
}

The compiler can deduce T for Foo1, as it is explicitly defined as a parameter. It also can infer T & AA in Foo2, although T is only there implicitly as an generic argument.
But if it can handle Foo2, why not Foo3?


TIA,
Andy
--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40@*NO*SPAM*gmx.net
.



Relevant Pages

  • Re: Code Review - is this code shit
    ... I feel free to deduce from your lame ... competent enough to provide a compiler, by simple retargeting, ... When a wheel needs reinventing, ...
    (comp.lang.c)
  • Re: Code Review - is this code shit
    ... I feel free to deduce from your lame ... competent enough to provide a compiler, by simple retargeting, ... When a wheel needs reinventing, ...
    (comp.lang.c)
  • Inline functions and warning
    ... My compiler allows inline expansion with #pragma. ... #pragma inline foo1, foo2 ...
    (comp.lang.c)
  • Re: C# Nullable types
    ... Not if it's a local variable - you'll get a compiler error saying it's ... will test true for null with an unassigned variable. ... mind that instance variables of reference types are considered ...
    (microsoft.public.dotnet.framework)
  • Re: Compiler trick
    ... This is one of the reasons why I think it important to emphasise not just that ... the compiler /doesn't/ deduce these things, but that /it is not allowed to/. ...
    (comp.lang.java.programmer)