frank
08/21/2020, 4:43 AMjava.nio
in Kotlin and I have a question:
Why don't work member references
to function is this case? *I know that with Files.isDirectory(it)
work i*t
val stream = Files.list(folder)
val files = stream.filter { it != null && Files::isDirectory } // error in Files::isDirectory
.map { it!! }
Error Msg:
Type mismatch.
Required: Boolean
Found: KFunction2<Path!, Array<(out) LinkOption!>!, Boolean>PD: Sorry, if this topic doesn't have to be in this channel, but I didn't find JVM channel.
jbnizet
08/21/2020, 5:43 AMa && b
, both a
and b
must be booleans. it != null
is a boolean expression, but Files::isDirectory
is not: it’s a function reference.jbnizet
08/21/2020, 5:47 AMfilter
, because that’s what it expects, but that’s not what you’re doing. I.e. fileStream.filter(Files::isDirectory)
would be corrrect.frank
08/21/2020, 6:05 AMfileStream.filter(Files::isDirectory) would be corrrect.@jbnizet , Work it this but I can't obtain
it
in the scope of filter()
function if I add Predicate inside of filter I can obtain it
but fail Files::isDirectory
.
val stream = Files.list(folder)
val files = stream.filter(Files::isDirectory)//I cant obtain it
map { it!! }.toList()
//Files::isDirectory fail in predicate
files = stream.filter(Predicate { it != null && Files::isDirectory })
.map { it!! }.toList()
jbnizet
08/21/2020, 6:08 AM&&
, a boolean and a function. Files::isDirectory
is not a boolean. It’s a function which returns a boolean. To get a boolean, you need to call the function: it != null && Files.isDirectory(it)
. When using stream.filter(Files::isDirectory)
, it’s correct because filter expects a function which takes a Path and returns a boolean as argument, and Files::isDirectory is such a function.jbnizet
08/21/2020, 6:11 AMfrank
08/21/2020, 6:21 AMjbnizet
08/21/2020, 6:25 AMfrank
08/21/2020, 6:30 AM