Ray Rahke
05/08/2024, 6:54 AMobj::class
to check if objects are instances of a type
abstract class Base { }
class SubA : Base()
fun fn(obj: Base, t: KClass<out Base>) {
if (obj::class == t) {}
}
fun main() {
val a = SubA()
fn(a, SubA::class)
}Is there any possible way to mimic this behavior by using ::Foo
instead
abstract class Base { }
class SubA : Base()
// v different
fun fn(obj: Base, t: KFunction<Base>) {
if (obj::class == t) {}
}
fun main() {
val a = SubA()
fn(a, ::SubA) // <- different
}
Goetz Markgraf
05/08/2024, 6:57 AMFoo
is a type that is compared with is
::Foo
is a reference to a function. Why would you want to compare in object with a function?
What you can do:
val x = ::Foo
if (x == ::Foo)
Here you are comparing two function references. Does that help?Ray Rahke
05/08/2024, 6:59 AMSubA::class
Ray Rahke
05/08/2024, 6:59 AMRay Rahke
05/08/2024, 6:59 AMabstract class Base { }
class SubA : Base()
fun fn(obj: Base, t: KClass<out Base>) {
if (obj::class == t) {}
}
fun main() {
val a = SubA()
fn(a, SubA::class)
desired
abstract class Base { }
class SubA : Base()
// v different
fun fn(obj: Base, t: KFunction<Base>) {
if (obj::class == t) {}
}
fun main() {
val a = SubA()
fn(a, ::SubA) // <- different
}
Ray Rahke
05/08/2024, 7:00 AMSubA::class
is very verbose and uglyRay Rahke
05/08/2024, 7:00 AMRay Rahke
05/08/2024, 7:00 AMRay Rahke
05/08/2024, 7:01 AMentity.hasComponents(::Physics, ::Graphics, ::Health)
is much cleaner than
entity.hasComponents(PHysics::class, Graphics::class, Health::class)
Ray Rahke
05/08/2024, 7:02 AMRay Rahke
05/08/2024, 7:02 AMGoetz Markgraf
05/08/2024, 7:02 AM::class
is ugly, but that depends on taste.
As I have said, ::something
is a pointer to a function not to a type.Ray Rahke
05/08/2024, 7:02 AMRay Rahke
05/08/2024, 7:03 AMRay Rahke
05/08/2024, 7:03 AMPhysics
Ray Rahke
05/08/2024, 7:03 AMAs I have said,yeah so Is there a way to say "this object's types constructor is equivalent to this function"?is a pointer to a function not to a type.::something
Ray Rahke
05/08/2024, 7:04 AMif (::(obj::class) == fn)
where fn is ::Foo
Goetz Markgraf
05/08/2024, 7:05 AMin typescript you can just say the classTypescript does not really have classes, the concepts cannot be compared. Maybe there is an alternative. Do all your type inherit from a single base type? Then you might add a function for comparison there taht “redirects” to an enum. Of course, you with have to manually sync you enum to your types. If your types seldom change, that might be another way.
Ray Rahke
05/08/2024, 7:06 AM::class
adds upRay Rahke
05/08/2024, 7:06 AMRay Rahke
05/08/2024, 7:06 AMGoetz Markgraf
05/08/2024, 7:06 AMRay Rahke
05/08/2024, 7:06 AMComponent
classRay Rahke
05/08/2024, 7:07 AMRiccardo Lippolis
05/08/2024, 7:08 AMabstract class Base
class SubA : Base()
inline fun <reified T : Base> fn(obj: Base) {
if (obj is T) {}
}
fun main() {
val a = SubA()
fn<SubA>(a)
}
but that does mean inlining the function, and you'd really have to wonder if it's worth it all just because you find ::class
ugly...Ray Rahke
05/08/2024, 7:10 AMval Monitor = MonitorEntities(Physics::class, Gravity::class, Spatial::class, Material::class, Mesh::class)
Ray Rahke
05/08/2024, 7:12 AMval Monitor = MonitorEntities<Physics, Gravity, Spatial, Material, Mesh>()
but is that possible?Riccardo Lippolis
05/08/2024, 7:13 AMRay Rahke
05/08/2024, 7:15 AMfn(Physics)
if they have a companion object.
...but kotlin outlaws putting a single companion object on a base class to be inherited for all subclasses (arbitrarily, because they thought it was "too confusing"). So I would have to redundantly redefine the exact same companion object for every single subclass. (so, hundreds of times realistically)Ray Rahke
05/08/2024, 7:18 AMRay Rahke
05/08/2024, 7:18 AMRay Rahke
05/08/2024, 7:18 AMTim McCormack
05/08/2024, 11:13 PMval CSubA = SubA::class
as a global?