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
Adam S
01/30/2024, 7:31 PM
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
Klitos Kyriacou
01/31/2024, 9:12 AM
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