You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
final mapped =mapNotNullTo(mutableListOf<R>(), transform);
// TODO ping dort-lang/sdk team to check type bug
// When in single line: type "DartMutableList<String>' is not a subtype of type 'Null"
return mapped;
}
/// Applies the given [transform] function to each element in the original collection
/// and appends only the non-null results to the given [destination].
CmapNotNullTo<R, CextendsKtMutableCollection<R>>(
C destination, R?Function(T?) transform) {
for (final item in iter) {
final result =transform(item);
if (result !=null) {
destination.add(result);
}
}
return destination;
}
}
This causes a build error with the following code:
listOf("not null").mapNotNull((it) => it.startsWith("not") ? it :null); // error!!listOf("not null", null).mapNotNull((it) => it?.startsWith("not") ==true? it :null);
The following code in Kotlin did not cause a build error .
listOf("not null").mapNotNull { if (it.startsWith("not")) it elsenull }
listOf("not null", null).mapNotNull { if (it?.startsWith("not") ==true) it elsenull }
It looks like the signature of RequireNoNullsKtIterableExtension.mapNotNull is wrong.
Suggestion
Quoting the implementation of mapNotNull from kotlin.collections:
/** * Returns a list containing only the non-null results of applying the given [transform] function * to each element in the original collection. * * @sample samples.collections.Collections.Transformations.mapNotNull*/publicinlinefun <T, R:Any> Iterable<T>.mapNotNull(transform: (T) ->R?): List<R> {
return mapNotNullTo(ArrayList<R>(), transform)
}
/** * Applies the given [transform] function to each element in the original collection * and appends only the non-null results to the given [destination].*/publicinlinefun <T, R:Any, C:MutableCollection<inR>> Iterable<T>.mapNotNullTo(destination:C, transform: (T) ->R?): C {
forEach { element -> transform(element)?.let { destination.add(it) } }
return destination
}
The following code seems fine.
extension _<T>onKtIterable<T> {
/// Returns a list containing the results of applying the given [transform] function /// to each element in the original collection.KtList<R> mapNotNull2<R>(R?Function(T) transform) {
final mapped =mapNotNullTo(mutableListOf<R>(), transform);
// TODO ping dort-lang/sdk team to check type bug// When in single line: type "DartMutableList<String>' is not a subtype of type 'Null"return mapped;
}
/// Applies the given [transform] function to each element in the original collection /// and appends only the non-null results to the given [destination].CmapNotNullTo<R, CextendsKtMutableCollection<R>>(C destination, R?Function(T) transform) {
for (final item in iter) {
final result =transform(item);
if (result !=null) {
destination.add(result);
}
}
return destination;
}
}
Note that the code above is an extension of KtIterable<T>.
I thought mapNotNull should be defined in extension KtIterableExtensions<T> on KtIterable<T> or extension ChainableKtIterableExtensions<T> on KtIterable<T>.
Version
This problem happened when I upgraded from 0.9.1 to 1.0.0.
kt_dart: ^0.9.1 -> ^1.0.0
The text was updated successfully, but these errors were encountered:
Problem
kt.dart/lib/src/collection/kt_iterable.dart
Line 1574 in 80402d4
kt.dart/lib/src/collection/kt_iterable.dart
Lines 1646 to 1667 in 80402d4
This causes a build error with the following code:
The following code in Kotlin did not cause a build error .
It looks like the signature of RequireNoNullsKtIterableExtension.mapNotNull is wrong.
Suggestion
Quoting the implementation of mapNotNull from kotlin.collections:
The following code seems fine.
Note that the code above is an extension of
KtIterable<T>
.I thought mapNotNull should be defined in
extension KtIterableExtensions<T> on KtIterable<T>
orextension ChainableKtIterableExtensions<T> on KtIterable<T>
.Version
This problem happened when I upgraded from 0.9.1 to 1.0.0.
The text was updated successfully, but these errors were encountered: