ageery
11/21/2018, 9:41 PMfun <C: Any, T: C?> List<T>.nonNull(sub: C) = map { it ?: sub }
. However, the compiler can't infer the type of C if there is a null in the list. For example, this doesn't compile: val x = listOf("x", null).nonNull("y")
. You have to explicitly specify the types: val x = listOf("x", null).nonNull<String, String?>("y")
. Is there something wrong with my method signature that prevents the compiler from inferring the types?serebit
11/21/2018, 9:42 PMserebit
11/21/2018, 9:43 PMfun <T : Any, R : T> List<T?>.replaceNulls(sub: R) = map { it ?: sub }
serebit
11/21/2018, 9:44 PM<T : C>
and List<T?>
, not <T : C?>
and List<T>
ageery
11/21/2018, 9:50 PMserebit
11/21/2018, 9:51 PMageery
11/21/2018, 10:09 PMinterface I<C : Any, T : C?> {
val list: List<T>
fun indexAt(index: Int): C
}
class C<C : Any, T : C?>(override val list: List<T>, private val defaultValue: C) : I<C, T> {
override fun indexAt(index: Int): C = list[index] ?: defaultValue
}
val c1 = C(listOf("a"), "z")
val list: List<String> = c1.list
val c2 = C<String, String?>(listOf("a", null), "z")
val s: List<String?> = c2.list
serebit
11/21/2018, 10:14 PMserebit
11/21/2018, 10:16 PMinterface Interface<T : Any> {
val list: List<T>
}
class Class<T : Any>(originalList: List<T?>, private val defaultValue: T) : Interface<T> {
override val list = originalList.map { it ?: defaultValue }
}
val c1 = Class(listOf("a"), "z")
val list: List<String> = c1.list
val c2 = Class(listOf("a", null), "z")
val s: List<String> = c2.list
ageery
11/21/2018, 10:20 PMindexAt
method. So the type for s should be List<String?>
not List<String>
.Andreas Sinz
11/21/2018, 10:25 PMC
serebit
11/21/2018, 10:28 PMinterface Interface<T : Any> {
val list: List<T?>
}
class Class<T : Any>(override val list: List<T?>, private val defaultValue: T) : Interface<T> {
override val list = originalList.map { it ?: defaultValue }
fun indexAt(index: Int) = list[index] ?: defaultValue
}
val c1 = Class(listOf("a"), "z")
val list = c1.list
val c2 = Class(listOf("a", null), "z")
val s = c2.list
ageery
11/21/2018, 11:36 PMList<String?>
, right? It should be List<String>
. I want the type of Class.list to be the type of the list that is passed in to the constructor.serebit
11/21/2018, 11:40 PMList<T>
or List<T?>
, the variable will be stored as List<T?>
.ageery
11/22/2018, 12:41 AMserebit
11/22/2018, 12:46 AMserebit
11/22/2018, 12:48 AM