C#C
C#2y ago
newobject

Inferring arguments to a method

Is there any way to get C# to infer "A" here correctly?

public interface IScene<A> {
    public void Init(A args);
}


class MyType : IScene<string> {
  public void Init(string args) { ... }
}

...

public T Spawn<T, A>(PackedScene scene, A args) where T : class, IScene<A> {
    T t = scene.Instantiate<T>();
    t.Init(args);
    return t;
}

...

MyType myType = Spawn<MyType>(scene, "hi");  // compiler complains about omitting type of A here


Why doesn't it infer that A is string in this case?
Was this page helpful?