Bug 34898

Summary: Doesn't compile dynamic on nested templates whereas visual studio does ?
Product: [Mono] Compilers Reporter: smallwill <william.ribault>
Component: C#Assignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: mono-bugs+mono
Priority: ---    
Version: 4.0.0   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Tags: Is this bug a regression?: ---
Last known good build:

Description smallwill 2015-10-15 06:03:56 UTC
I'm using Mono C# compiler version 4.0.4.0
I first asked about it on stackoverflow http://stackoverflow.com/questions/33144952/in-c-sharp-how-to-dynamically-select-the-type-of-the-generic-function-to-call
Where I learned that it works under visual studio.

In C#, I have some generic function. I have a list of dynamic objects with which I want to call the rightly typed function
It works in simple cases, but doesn't compile as soon as templates are nested.
How to make it work in the nested templates case?

void f1<T>(T a )
{
  Console.WriteLine( typeof(T) );
}

void f2<T>(List<T> a )
{
  Console.WriteLine( typeof(T) );
}

void f3<T>(List<List<T>> a )
{
  Console.WriteLine( typeof(T) );
}

[Test ()]
public void TestDynamic()
{
  var l = new List<dynamic>();
  l.Add(3);
  l.Add("hello");
  //This compiles and works fine 
  foreach (var i in l)
    f1(i);
  //prints System.Int32
  //prints System.String

  var l2 = new List<dynamic>();
  l2.Add(new List<int>{3});
  l2.Add(new List<string>{"hello" } );
  //This compiles and works fine
  foreach (var i in l2)
    f2(i);
  //prints System.Int32
  //prints System.String

  var l3 = new List<dynamic>();
  l3.Add(new List<List<int>>{new List<int>{3} });
  l3.Add(new List<List<string>> {new List<string>{"hello" } });
  //This doesn't compile : "The type argument for method f3 cannot be inferred from the usage, try specifying the type arguments explicitly
  foreach (var i in l3)
    f3(i);
}
Comment 1 Marek Safar 2015-10-20 18:03:55 UTC
Fixed in master