poohbar
07/09/2021, 9:20 PMobject
can’t do generics?
class Mapper<T : Account> : RecordMapper<Record, T> {
override fun map(record: Record?): T? {
TODO("Not yet implemented")
}
}
Even better, I really just need a function, where the input is Record
and the output is T: Account
how do I declare that?poohbar
07/09/2021, 9:21 PMprivate val MAPPER: (Record) -> Account = { record: Record ->
TODO("Not yet implemented")
}
But the output should be T: Account
, not Account
Dominaezzz
07/09/2021, 9:23 PMemptyList<T>()
.poohbar
07/09/2021, 9:25 PMfun <T : Account> mapToAccount(record: Record): T {
TODO("Not yet implemented")
}
poohbar
07/09/2021, 9:25 PM::mapToAccount
poohbar
07/09/2021, 9:30 PMfun <T : Account> mapToAccount(record: Record): T {
val x: Account = Account(...)
return x // error here
}
poohbar
07/09/2021, 9:31 PMAccount
fit into T
? 😮Dominaezzz
07/09/2021, 9:33 PMin
or out
.poohbar
07/09/2021, 9:33 PMVariance annotations are only allowed for type parameters of classes and interfaces
poohbar
07/09/2021, 9:35 PMopen class Person
fun <T: Person> foo(): T {
return Person()
}
diesieben07
07/09/2021, 9:40 PM::mapToAccount
the T
gets bound to a concrete value.
For example:
val foo: (Record) -> SomeAccountSubclass = ::mapToAccount
binds T
as SomeAccountSubclass
.
(Record) -> T
is not a valid type.diesieben07
07/09/2021, 9:40 PMT
.diesieben07
07/09/2021, 9:44 PMT
is always the same and as such redundant...ephemient
07/09/2021, 9:55 PMobject Mapper : RecordMapper<Record, Nothing>
but there isn't too much you could do with thatdiesieben07
07/09/2021, 9:57 PMjbnizet
07/09/2021, 10:05 PMval doctor: Doctor = foo<Doctor>()
.` So returning an instance of Person is incorrect: a Doctor is a Person, but a Person is not a Doctor.ephemient
07/09/2021, 10:47 PMNothing
works: it is a subtype of every other type. of course, there are no actual instances of Nothing, so returning is not possible (only abnormal termination or non-termination), but it's the only type that workspoohbar
07/10/2021, 10:26 PMemptyList()
is implemented as:
internal object EmptyList : List<Nothing>
poohbar
07/10/2021, 10:26 PMephemient
07/11/2021, 1:35 AMList<Nothing>
is a List<T>
because Nothing
is a T
and List<out T>
is covariantephemient
07/11/2021, 1:37 AMget(): T
, for any type T
, out of a `List<Nothing>`… with type safety. of course, at runtime, you can't "get" anything from an empty list, but that doesn't matter to the types