def parseFileByName(fileName: String): DimacsFile = {
import scala.util.Using
Using(Source.fromFile(fileName)) { source =>
parseBufferedContent(source.buffered)
} .get // This will throw if it was an error.
}
BTW, if the file is really big, you may consider using a Stream like fs2 or AkkaStreams.
As I understand, the .get obviates the need to pattern match Success vs Failure, but the problem of the missing implicit persists. Is this just a wrong warning from IntelliJ?
Looks like it is a real error. When I try to compile, I get the following error.
could not find implicit value for evidence parameter of type util.Using.Releasable[scala.collection.BufferedIterator[Char]]
Using(Source.fromFile(fileName).buffered) { reader =>
BTW, I’m always afraid to use the _ short hand for anonymous functions because I don’t understand how it decides what the function body is. How far out does it go?
Is _.buffered the function
or is parseBufferedContent(_.buffered) the function
or is { parseBufferedContent(_.buffered) }.get
or is Using(Source.fromFile(fileName)) { parseBufferedContent(_.buffered) }
or is Using(Source.fromFile(fileName)) { parseBufferedContent(_.buffered) }.get
Some of those choices are of course ridiculous, but since I don’t know the rule, and I’ve been burned by it in the past, I tend to use
In my experience, it doesn’t go beyond a pair of braces or parentheses, except for (_).
I wouldn’t be afraid, because if the expansion is not what you expected, the compiler will almost certainly complain. Because it has to infer the type of each underscore, it will only typecheck if used in a context where a function of known argument types is expected.
If I’m not mistaken f(_,12) is a function, but f(_.get) calls f with a function as argument. Maybe I’m mistaken, but otherwise, that violates the idea that it stops at paren boundaries.
It’s basically what @curoli said. It doesn’t go beyond the expression within a pair of parentheses except when it is the expression in the parentheses. _ is always a placeholder in a larger expression, it’s not an identity function.
Oh sweet! I need to remember that for future questions. I am already too used to cats but I usually try to (at least additionally) provide an std-only answer.