C
C#2y 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
nutOP2y ago
changing List<T> to List<Component> in the return type fixes it, but isnt what i want
Jimmacle
Jimmacle2y ago
what type does GetAll return?
nut
nutOP2y ago
List<Component> i had the same issue back there
Jimmacle
Jimmacle2y 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
nutOP2y 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
nutOP2y ago
so ive got this interface
nut
nutOP2y ago
actually im just gonna hide the dictionary so it doesnt have to be generalized
Jimmacle
Jimmacle2y 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
nutOP2y 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
Accord2y 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