Ray Rahke
05/07/2024, 2:38 PMx: Set<Foo> or that requires that all elements of the set be a Foo (edCLOVIS
05/07/2024, 2:42 PMFoo and the other elements of the set?Ray Rahke
05/07/2024, 4:06 PMRay Rahke
05/07/2024, 4:07 PMRay Rahke
05/07/2024, 4:07 PMKlitos Kyriacou
05/07/2024, 4:33 PMPair<Foo, Set<Any>> might be a better fit.Kirill Grouchnikov
05/07/2024, 6:06 PMSet<Foo> is not saying “all elements are Foo and there is at least 1 element in there”. That’s just not what generics are for.Kirill Grouchnikov
05/07/2024, 6:07 PMCLOVIS
05/07/2024, 7:00 PMi want a solution that does not require there to be any type relation between elements of the setThat is, by definition,
Set<Any> .Ruckus
05/07/2024, 7:43 PMRay Rahke
05/08/2024, 2:12 AMEntity has a components set MutableSet<Component> where Compoenent is an abstract base class. I have functions that receive x: Entity, but have no knowledge that this entity has a Physics componentRay Rahke
05/08/2024, 2:13 AMfun fn(x: Entity<but who is guaranteed to have a Physics component in their .components set>) {
// do things that work on the assumption that entities has a Physics component
}Ray Rahke
05/08/2024, 2:20 AMRay Rahke
05/08/2024, 2:24 AM.components is guaranteed to have a Component of subclass type Physics, but may have other Component objects too like Sprite and Health"Ray Rahke
05/08/2024, 2:34 AMfn SomethingRelatingToPhysics(entity: Entity) {
if (entity.hasComponent<Physics>() == false) {
throw error
}
physics.GetConfidently<Physics>()
}Kirill Grouchnikov
05/08/2024, 3:55 AMKirill Grouchnikov
05/08/2024, 3:57 AMKirill Grouchnikov
05/08/2024, 3:59 AMRay Rahke
05/08/2024, 5:23 AMHow do you see this code looking like if Kotlin generics supported such a case?If You could annotate a Set<> has definitely having at least some Foo element, then it is trivial
Ray Rahke
05/08/2024, 5:24 AMEntity<Physics> and then pass that argument down into the val components: MutableSet<At least one Physics>Szymon Jeziorski
05/08/2024, 8:59 AMMutableSet<Any, but with at least one Physics>, it pretty much wouldn't be possible for the compiler to handle such check by itself with built-in collection types. MutableSet is an interface, it has no constructor, there can by many implementations to it. Sets can be created empty, with single value and with multiple values. All mutations happen in runtime and can be result of conditions which are also evaluated in runtime, so it's pretty much not possible for the compiler to track collections states deterministically.
It looks to me as your best bet is to either use Pair<Physics, Set<Any>> as previously suggested, or even better, create your own dedicated class that defines the shape of the contract you need and use it wherever appropriate.Tim McCormack
05/08/2024, 11:07 PMTim McCormack
05/08/2024, 11:08 PM