Tomasz Krakowiak
03/30/2021, 6:09 PMinterface ListModification<out E> {
fun applyTo(target: MutableList<in E>)
fun <T> apply(target: List<T>) : List<T>
where E : T
}
nanodeath
03/30/2021, 6:14 PMinterface ListModification<E> {
fun applyTo(target: MutableList<E>)
fun apply(target: List<E>) : List<E>
}
does this not work for you?nanodeath
03/30/2021, 6:14 PMTomasz Krakowiak
03/30/2021, 6:34 PMnanodeath
03/30/2021, 6:39 PMinterface ListModification<E> {
fun applyTo(target: MutableList<E>)
fun <T : E> apply(target: List<E>) : List<T>
}
?nanodeath
03/30/2021, 6:40 PME
== T
, should make the types shake outNir
03/30/2021, 6:42 PMT
for applyNir
03/30/2021, 6:42 PMTomasz Krakowiak
03/30/2021, 6:43 PMNir
03/30/2021, 6:44 PMTomasz Krakowiak
03/30/2021, 6:44 PMnanodeath
03/30/2021, 6:44 PMTomasz Krakowiak
03/30/2021, 6:45 PMobject Clear : ListManipulation<Nothing>
Tomasz Krakowiak
03/30/2021, 6:46 PMNir
03/30/2021, 6:48 PMNir
03/30/2021, 6:48 PMTomasz Krakowiak
03/30/2021, 6:48 PMfun <T> List<T>.apply(change: ListModification<T>)
(T is contravariant in this case)Tomasz Krakowiak
03/30/2021, 6:49 PMfun <T> List<T>.apply(change: ListModification<T>) : List<T>
Nir
03/30/2021, 6:51 PMTomasz Krakowiak
03/30/2021, 6:51 PMNir
03/30/2021, 6:52 PMNir
03/30/2021, 6:52 PMNir
03/30/2021, 6:53 PMpublic interface PersistentList<out E> : ImmutableList<E>, PersistentCollection<E> {
/**
* Returns a new persistent list with the specified [element] appended.
*/
override fun add(element: @UnsafeVariance E): PersistentList<E>
Nir
03/30/2021, 6:53 PMnanodeath
03/30/2021, 6:54 PMNir
03/30/2021, 6:54 PMout E
but you still have functions, that, by their signatures, could read elements passed by the user, so you need to basically reassure the type system that you pinky swear not to actually read those elementsTomasz Krakowiak
03/30/2021, 6:56 PMsealed class MetaChange : ListModification<Nothing> {
override fun applyTo(target: MutableList<in Nothing>) {
applyMetaChange(target)
}
abstract fun <E> applyMetaChange(target: MutableList<E>)
}
Nir
03/30/2021, 6:56 PMTomasz Krakowiak
03/30/2021, 6:57 PMdata class Swap(
val aIndex: Int,
val bIndex: Int
) : PermutationChange(), StandardChange {
override fun <T> applyMetaChange(target: MutableList<T>) {
val element = target.removeAt(aIndex)
target.add(bIndex, element)
}
}
Tomasz Krakowiak
03/30/2021, 6:58 PMTomasz Krakowiak
03/30/2021, 7:00 PMTomasz Krakowiak
03/30/2021, 7:18 PMTomasz Krakowiak
03/30/2021, 7:22 PMNir
03/30/2021, 7:23 PMNir
03/30/2021, 7:23 PMNir
03/30/2021, 7:24 PMNir
03/30/2021, 7:24 PMTomasz Krakowiak
03/30/2021, 7:24 PMNir
03/30/2021, 7:24 PM