C
C#10mo ago
nut

❔ generic class in generic method

im trying to have this method:
public void AddPrefab<T, C>(C args) where T : Prefab<C> where C : PrefabArgs
{
T.Create(this, args);
}
public void AddPrefab<T, C>(C args) where T : Prefab<C> where C : PrefabArgs
{
T.Create(this, args);
}
where each thing that inherits Prefab has a PrefabArgs generic type thing, like, a Prefab "player" could be Player<PlayerPrefabArgs>, and you can call this method like
AddPrefab<Player>(new PlayerPrefabArgs());
AddPrefab<Player>(new PlayerPrefabArgs());
but the problem im having is this just isnt working how id like it to, i have to do
AddPrefab<Player, PlayerPrefabArgs>(...);
AddPrefab<Player, PlayerPrefabArgs>(...);
like it doesnt know how to infer the type or something
8 Replies
mtreit
mtreit10mo ago
How could it infer the type of T when you only pass it something of type C?
nut
nut10mo ago
because i tell it T
mtreit
mtreit10mo ago
T is a placeholder for an actual type
nut
nut10mo ago
player is class Player : Prefab<PlayerPrefabArgs>
reflectronic
reflectronic10mo ago
you cannot have it partially infer the generic arguments you either have to specify none of them or all of them
nut
nut10mo ago
thats annoying so this method is just either impossible or too verbose is there any other way i can do a AddPrefab<Player>(PlayerPrefabArgs)
public void AddPrefab<T>(PrefabArgs args) where T : Prefab<PrefabArgs>
{
T.Create(this, args);
}
public void AddPrefab<T>(PrefabArgs args) where T : Prefab<PrefabArgs>
{
T.Create(this, args);
}
this doesnt work either i really dont wanna do AddPrefab<Player, PlayerPrefabArgs>(new PlayerPrefabArgs) every time
Florian Voß
Florian Voß10mo ago
why not?
Accord
Accord10mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts