In the following code: ```class A<Any>(val a: Any)...
# getting-started
n
In the following code:
Copy code
class A<Any>(val a: Any)

fun <T: Comparable<T>> A(a: T): A<T> = 
    A(a) // <--- how to call the constructor of class A instead of calling function A() recursively?
Is this the most correct solution?
Copy code
fun <T: Comparable<T>> A(a: T): A<T> =
    A<Any>(a) as A<T>
Thanks. EDIT: it is a theoretical question, just to know the edge cases of the language ;)
😱 2
a
Thanks, I hate it! Not ideal - but you could use a typealias
Copy code
data class A<Any>(val a: Any)

typealias AConstructor<T> = A<T>

fun <T: Comparable<T>> A(a: T): A<T> =
    AConstructor(a)

fun main() {
    println(A("x"))
    println(AConstructor(1))
}
😄 1
🙏 1
1
k
By the way, do you realise that in the declaration
class A<Any>(val a: Any)
, the word
Any
doesn't refer to the well-known type
Any
? It is just a generic parameter. To avoid confusion, name it something like
T
instead.
👍🏻 1
n
Yes, it should have been
Copy code
class A<T: Any>(val a: T)
Sorry for the confusion...