LPeter1997
LPeter1997
CC#
Created by Clonkex on 9/5/2023 in #help
✅ Return List<Entity> from method with return type of List<T> where T : Entity
55 replies
CC#
Created by Clonkex on 9/5/2023 in #help
✅ Return List<Entity> from method with return type of List<T> where T : Entity
And that C# only supports them through interfaces and even then, only the direction where it's safe
55 replies
CC#
Created by Clonkex on 9/5/2023 in #help
✅ Return List<Entity> from method with return type of List<T> where T : Entity
Your problem here really is generic variance
55 replies
CC#
Created by Clonkex on 9/5/2023 in #help
✅ Return List<Entity> from method with return type of List<T> where T : Entity
The first one is a runtime cast The second is just an upcast The third should also be a runtime cast, so I think the snippet you sent should compile technically
55 replies
CC#
Created by Clonkex on 9/5/2023 in #help
✅ Return List<Entity> from method with return type of List<T> where T : Entity
In that case, I'd do this:
public static IEnumerable<T> GetEntitiesByType<T>() where T : Entity
{
if (!EntitiesByType.TryGetValue(typeof(T), out var entities))
{
entities = new List<Entity>();
EntitiesByType.Add(typeof(T), entities);
}
return entities.OfType<T>();
}
public static IEnumerable<T> GetEntitiesByType<T>() where T : Entity
{
if (!EntitiesByType.TryGetValue(typeof(T), out var entities))
{
entities = new List<Entity>();
EntitiesByType.Add(typeof(T), entities);
}
return entities.OfType<T>();
}
You can also store the lists as object and then instantiate them as List<T>, cast them as such when accessing them. I personally like that less 😄
55 replies
CC#
Created by Clonkex on 9/5/2023 in #help
✅ Return List<Entity> from method with return type of List<T> where T : Entity
It's a less nasty cast than if we have to go the full type-map route
55 replies
CC#
Created by Clonkex on 9/5/2023 in #help
✅ Return List<Entity> from method with return type of List<T> where T : Entity
I wanna be sure before I propose a solution because if not, it's easily fixed by returning .OfType<T>() (with IEnumerable<T> as a return type)
55 replies
CC#
Created by Clonkex on 9/5/2023 in #help
✅ Return List<Entity> from method with return type of List<T> where T : Entity
So from the outside, do you expect me to do GetEntitiesByType<Foo>().Add(new())
55 replies
CC#
Created by Clonkex on 9/5/2023 in #help
✅ Return List<Entity> from method with return type of List<T> where T : Entity
Oh also, do you want to allow the user calling this to mutate the list, like adding and removing entities?
55 replies
CC#
Created by Clonkex on 9/5/2023 in #help
✅ Return List<Entity> from method with return type of List<T> where T : Entity
Is it going to be called often, are entities of a certain type often queried? Are there many kinds of entities?
55 replies
CC#
Created by Clonkex on 9/5/2023 in #help
✅ Return List<Entity> from method with return type of List<T> where T : Entity
What you want can be solved tho, just with a bit more type erasure, or with collection manipulation. What's the end goal of this method, where would it be used?
55 replies
CC#
Created by Clonkex on 9/5/2023 in #help
✅ Return List<Entity> from method with return type of List<T> where T : Entity
The problem is, you instantiated a List<Entity>. Let's assume you want to promise it only stores some derived type T of Entity and you lend it out as List<T>. The problem is, internally this could have contained any Entity, you can not be sure it's not just Ts in there, breaking the type system
55 replies
CC#
Created by Clonkex on 9/5/2023 in #help
✅ Return List<Entity> from method with return type of List<T> where T : Entity
BUT C# only supports variance where it's more or less safe (except arrays but that's legacy behavior at this point)
55 replies
CC#
Created by Clonkex on 9/5/2023 in #help
✅ Return List<Entity> from method with return type of List<T> where T : Entity
What you are asking for here is not unheard of, it's called variance, when T<U> can become T<V> in some cases where U and V are in some relation (based on the nature of the relation this is either called co- or contravariance)
55 replies
CC#
Created by Clonkex on 9/5/2023 in #help
✅ Return List<Entity> from method with return type of List<T> where T : Entity
Well since this looks like a dictionary value, I suppose you shoved them all in a List<Entity>, so your dict is likely Dictionary<Type, List<Entity>>, right?
55 replies
CC#
Created by Clonkex on 9/5/2023 in #help
✅ Return List<Entity> from method with return type of List<T> where T : Entity
So entities is List<Entity>, right?
55 replies
CC#
Created by Omar on 9/3/2023 in #help
❔ Why is C# so slow on Linux? How can I speed it up?
Apparently you have been a professional since 1985 yet you are profiling a Hello World application lol
100 replies
CC#
Created by from I select q[0] on 8/31/2023 in #help
✅ unit testing ViemModels
There is not a lot you can/should be testing in the VM directly, ideally it's just an adaptation layer that might change a lot depending on the UI, so it might actually be disadvantageous to test it
9 replies
CC#
Created by from I select q[0] on 8/31/2023 in #help
✅ unit testing ViemModels
I'd honestly never test a VM, only the services themselves if they belong to the same codebase ofc
9 replies
CC#
Created by LPeter1997 on 8/30/2023 in #help
❔ Concurrent-exclusive scheduling with prioritizing exclusive execution
Uhh... I'll wait for TPL experts to answer 😅
7 replies