C
C#13mo ago
nut

❔ template function cant convert despite constraint

hi, so im working on a basic ECS kind of thing for my game, and im trying to make a method that returns a list<T> given a T, where T is a component. for some reason, it cant convert T to component despite the constraint that T will always be a component
10 Replies
nut
nut13mo ago
changing List<T> to List<Component> in the return type fixes it, but isnt what i want
Jimmacle
Jimmacle13mo ago
what type does GetAll return?
nut
nut13mo ago
List<Component> i had the same issue back there
Jimmacle
Jimmacle13mo ago
it should return List<T> the problem is you're constraining T to be a Component, but that doesn't mean a Component is a T
nut
nut13mo ago
well it looks like this problem goes really far back into my code to where i was trying to have a processor class for each component i made an abstract ComponentProcessor class with abstract methods that work with a Component type and then i made ComponentProcessor<T> to get more specific but i still need to refer to them all as ComponentProcessor not ComponentProcessor<T> so i can store them in a list together
nut
nut13mo ago
so ive got this interface
nut
nut13mo ago
actually im just gonna hide the dictionary so it doesnt have to be generalized
Jimmacle
Jimmacle13mo ago
based on the naming convention it definitely shouldn't be public it also generally sounds like an implementation detail and shouldn't be visible to consumers
nut
nut13mo ago
yeah i know i realized that as i was doing this i just made the components array private so i can manage every use of it
Accord
Accord13mo 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.