newobject
newobject
CC#
Created by newobject on 7/14/2024 in #help
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?
2 replies