Gustav Elmgren
08/16/2023, 9:44 AMclass Test<T : Comparable<T?>>(
val value: T
)
val a = Test<String?>("Hello")
val b = Test<String>("Hello")
//Neither of the above works
I want to create a class that can both handle String
and String?
as the type argument. And if I understand correct, String
is a sub type of String?
.
If I instead make it non-nullable:
class Test<T : Comparable<T>>(
val value: T
)
val a = Test<String?>("Hello")
val b = Test<String>("Hello")
//Now b works atleast, but not a.
Joffrey
08/16/2023, 9:51 AMString
is not a Comparable<String?>
and String?
is not comparable at allGustav Elmgren
08/16/2023, 9:52 AMval test: String? = null
val a = test == "hello"
The ==
operator seems to be the equals
method in public class String : Comparable<String>, CharSequence
Adam S
08/16/2023, 9:53 AMT
nullable, or the value?
For example:
class Test<T : Comparable<T>>(
val value: T?
)
val a = Test<String>("Hello")
val b = Test<String>(null)
Gustav Elmgren
08/16/2023, 9:53 AMT
nullable, so Test<String?>
should work as well as Test<String>
Adam S
08/16/2023, 9:54 AMGustav Elmgren
08/16/2023, 9:55 AMExpressionWithColumnType<T>
as well as ExpressionWithColumnType<T?>
.Gustav Elmgren
08/16/2023, 9:58 AMobject MyTable : LongIdTable(name = "MyTable") {
val nonNullable = varchar("nonNullable", 255)
val nullable = varchar("nullable", 255).nullable()
}
class Test<T : Comparable<T?>>(
expr: ExpressionWithColumnType<T>
)
val a = Test(MyTable.nonNullable)
val b = Test(MyTable.nullable)
Gustav Elmgren
08/16/2023, 10:15 AMobject MyTable : LongIdTable(name = "MyTable") {
val nonNullable = varchar("nonNullable", 255)
val nullable = varchar("nullable", 255).nullable()
}
class Test<T : Comparable<T>, S : T?>(
expr: ExpressionWithColumnType<in S>
)
val a = Test(MyTable.nonNullable)
val b = Test(MyTable.nullable)