How come I can write: ```val file = File("hello.tx...
# announcements
g
How come I can write:
Copy code
val 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:
Copy code
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-expression
d
The first one is a
when
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.
g
But the example doesn't show
==
against the subject. It's got
is Success
or
is HttpError
.
e
when-with-subject supports
==
comparison, `in`/`!in`, and `is`/`!is`.
d
Yes, you can use in and is as well, I forgot to mention that, sorry
e
https://youtrack.jetbrains.com/issue/KT-28359 there's a discussion about how to add other types of checks, but that gets wrapped up with other deeper pattern matching questions so any resolution will be a while off
for now, if you want to have boolean expressions as your cases, you need to use when without a subject
g
Thanks to the link to the YouTrack issue. I think I just found the
when
-
in
documentation, but if there's more/better docs for
in
somewhere, I'd love to know. https://kotlinlang.org/docs/basic-syntax.html#collections
e
the getting started docs don't cover every aspect of the language, just enough to get started
n
gotta say i find this case very very surprising
2