glenkpeterson
06/08/2021, 8:58 PMval file = File("hello.txt")
return when {
file.isFile -> Or.good(file)
else -> Or.bad(chapter.fileName)
}
But if I try to capture the when subject in a variable like this:
return when(val file = File("hello.txt")) {
file.isFile -> Or.good(file)
else -> Or.bad(chapter.fileName)
}
I get, "Incompatible Types: Boolean and File" on the file.isFile part?
The end of this section makes it look like I should be able to do this: https://kotlinlang.org/docs/control-flow.html#when-expressiondiesieben07
06/08/2021, 8:59 PMwhen without subject, so all branches are boolean expressions.
The second one is a when with subject of type File so all branches are compared using == against that subject. The first branch is of type Boolean and comparing File and Boolean makes no sense.glenkpeterson
06/08/2021, 9:00 PM== against the subject. It's got is Success or is HttpError.ephemient
06/08/2021, 9:01 PM== comparison, `in`/`!in`, and `is`/`!is`.diesieben07
06/08/2021, 9:01 PMephemient
06/08/2021, 9:02 PMephemient
06/08/2021, 9:04 PMglenkpeterson
06/08/2021, 9:07 PMwhen - in documentation, but if there's more/better docs for in somewhere, I'd love to know. https://kotlinlang.org/docs/basic-syntax.html#collectionsephemient
06/08/2021, 9:10 PMephemient
06/08/2021, 9:13 PMin keyword in the language specification:
https://kotlinlang.org/spec/statements.html#for-loop-statements
https://kotlinlang.org/spec/expressions.html#type-checking-and-containment-checking-expressions
https://kotlinlang.org/spec/expressions.html#when-expressionsNir
06/08/2021, 9:23 PM