ndtaylor
10/29/2015, 6:16 PMcrossinline
modifier.
I have an inline method and several other methods that delegate to that inline method.
private inline fun <T : BaseModel> doGetAllDescendantsWithPredicate(children: Iterable<BaseModel>,
recursive: Boolean,
crossinline predicate: (in BaseModel) -> Boolean): ArrayList<T> {
val results = ArrayList<T>()
val action: (BaseModel) -> Boolean = action@ { child ->
if (predicate(child)) {
results.add(child as T)
}
return@action false
}
iterate(children, recursive, action)
return results
}
public fun <T : BaseModel> getAllDescendantsWithPredicate(children: Iterable<BaseModel>,
crossinline predicate: (in BaseModel) -> Boolean): ArrayList<T> {
return doGetAllDescendantsWithPredicate(children, true, predicate)
}
public fun <T : BaseModel> getAllChildrenWithPredicate(children: Iterable<BaseModel>,
crossinline predicate: (in BaseModel) -> Boolean): ArrayList<T> {
return doGetAllDescendantsWithPredicate(children, false, predicate)
}
The problem with version 1.0 is that I can't mark the predicate as crossinline on the public functions, which are not inline functions. As I understand it, this means that I can't guarantee that whoever calls the public methods doesn't pass a lambda that contains non-local returns.
Does kotlin have some other validation that protects against this, or can I just no longer pass through lambdas to an inline function like before?