C
C#2mo 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
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?
1 Reply
333fred
333fred2mo ago
Because C# doesn't have partial type inference, and doesn't do inference from constraints
Want results from more Discord servers?
Add your server