How does typealias work?


#1
interface Order<T> {
     operator fun compareTo(o:T): Int
}
 
typealias Ord<T> = Order<T> // rename 
 
class Num<T> : Ord<T> {
     override fun compareTo(other: T): Int {
         TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
     }
}

all above is fine, but when I try something like

class Container1<T : Ord<T>>

it fails.But it’s fine for

class Container2<T : Order<T>>

Why is that?


#2

Puzzling!

A type parameter which implements a non-generic ‘typealiased’ interface doesn’t appear to be a problem as the following compiles fine:

interface I {
    fun someFun()
}

typealias J = I

class C<T: J>

The error message you get with the generic interface:

 error: type argument is not within its bounds: should be subtype of 'Any?'
class Container1<T : Ord<T>>
                         ^

is also strange (notice that the caret’s pointing to T not Ord) as I’d have thought that all types parameters in Kotlin, type-aliased or not, would be assumed to ultimately inherit from Any?.

So I think we will both have to wait for enlightenment on this apparent anomaly, probably from a JetBrains team member.


#3

It’s a bug. https://youtrack.jetbrains.com/issue/KT-19601

Thanks!