I'm trying to implement a class tree of related types (for example, e.g AST nodes) with a common abstract superclass. I'm trying to implement equals() on the subnodes so that different sub types are indeed different, but two like types can do a more introspective computation of equality. I've tried this:
abstract class Something { abstract fun equals(other:Something) : Boolean } class Foo(val value:Int):Something() { override fun equals(other:Something) : Boolean { return (other as Foo).value == value } } class Bar(val value:Int):Something() { override fun equals(other:Something) : Boolean { return (other as Bar).value == value } } fun main(args: Array<String>) { val foo1:Something = Foo(1) // if i don't type these as the abstract type val foo2:Something = Foo(1) // then I it won't even let me check val bar1:Something = Bar(42) // if bar1 == foo1, because they're inferred val bar2:Something = Bar(42) // as different types println("foo1 == foo2 ${foo1 == foo2}") // should print true println("bar1 == bar2 ${bar1 == bar2}") // should print true println("foo1 == bar1 ${foo1 == bar2}") // should print false } Unfortunately, all of the println's just show false though. What am I doing wrong?wrong?