Smart Сast Syntactic Sugar that I Would Like to Have in Kotlin

I tried to write a function that could be used with a set of classes that have the same method but not descendants of the same ancestor. For instance, JMenu and JTextField have addActionListener method. With the next approach it is possible to implement semi-dynamic type checking.
test.kt
class X { 
    fun foo() {} 
} 
 
class Z { 
    fun foo() {} 
} 
 
fun doSomehting (a:Any) { 
    when (a) { 
        is X -> a.foo() 
        is Z -> a.foo() 
    } 
}
this solution seems redundant. Because of autocompletion or accidentally, one can misprint the foo function. Additionally, for a large list, a lot of duplicates should be written for the each case. I would like to have something like this
 
    when (a) { 
        is X,Z -> a.foo() 
    }
For such a case Kotlin could generate the same byte code as for the case above.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>