how could I make this a singleton? it seems like `...
# getting-started
p
how could I make this a singleton? it seems like
object
can’t do generics?
Copy code
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?
Ideally I need something like this:
Copy code
private val MAPPER: (Record) -> Account = { record: Record ->
    TODO("Not yet implemented")
}
But the output should be
T: Account
, not
Account
d
Check out the implementation of
emptyList<T>()
.
p
I think I got it, I can just do this:
Copy code
fun <T : Account> mapToAccount(record: Record): T {
    TODO("Not yet implemented")
}
and then pass it as
::mapToAccount
well now i dont understand this
Copy code
fun <T : Account> mapToAccount(record: Record): T {
    val x: Account = Account(...)

    return x // error here
}
why doesn’t
Account
fit into
T
? 😮
d
I think you need
in
or
out
.
p
maybe i am putting it in the wrong place but it says:
Variance annotations are only allowed for type parameters of classes and interfaces
trivial example:
Copy code
open class Person

fun <T: Person> foo(): T {
    return Person()
}
d
Function references cannot be generic in this way. As soon as you do
::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.
So when you want to store it in a reference, you have to choose a concrete type for
T
.
So really you do not want a singleton, because a singleton by definition means that
T
is always the same and as such redundant...
👆 1
e
well, if you have your variance right, it would be fine to write a singleton
Copy code
object Mapper : RecordMapper<Record, Nothing>
but there isn't too much you could do with that
d
Yes, it could never return anything
j
@poohbar your function’s return type is T. T can be any class that extends Person: the caller of the function decides which class it is. So it could be a Child, an Adult, a Doctor... anything that extends Person. For example:
val doctor: Doctor = foo<Doctor>()
.` So returning an instance of Person is incorrect: a Doctor is a Person, but a Person is not a Doctor.
e
hence why
Nothing
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 works
p
maybe that’s why the
emptyList()
is implemented as:
Copy code
internal object EmptyList : List<Nothing>
strange
e
not strange at all,
List<Nothing>
is a
List<T>
because
Nothing
is a
T
and
List<out T>
is covariant
it may help you to think: you can
get(): 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