Sorry, me again, hope people don't mind. In the Je...
# getting-started
b
Sorry, me again, hope people don't mind. In the JetBrains tutorial it has the following code
*val* reader = FileReader("test.txt")
reader.*use* {
reader.read()
}
Which can be replaced by
FileReader("test.txt").use { reader -> read("something") }
Which I am confused by, what is "something" referring to?
j
I think that must be a mistake
☝️ 3
b
Certainly felt like they may be, so what should it be?
FileReader("test.txt").use { reader -> read() }
👎 1
🆗 1
m
You'll probably want to use
reader.read()
as
use
is does not scope
Reader
but passes it as a parameter (which is what the
reader ->
is).
1
j
Matthew is right. You should use either of those:
FileReader("test.txt").use { reader -> reader.read() }
FileReader("test.txt").use { it.read() }
r
Or, with a function reference
Copy code
FileReader("test.txt").use(Reader::read)
i
Since
use
passes its receiver as a parameter to the provided lambda, the following two are equivalent:
reader.use { reader.read() }
and
reader.use { it.read() }