First, I always find it distracting to check whether a boolean result “is equal to false” / “is equal to true”. It’s a boolean already and it reads more naturally to me using if (isWritable(x)) than if (isWritable(x) == true)).
Then, there is Either.cond which you could use instead, to reduce some verboseness:
But, using Unit as the right type inside the Either feels weird.
And, checking such conditions before actually doing something with the file / path is just asking for trouble, since the world can change between your check, and the action being carried out. Just try to use the file / directory as you intend and check for exceptions (you have to do this anyways), depending on the type of exception, return an “does not exist” error or some other message.
Agreed, I’d return the “validated” path on the rhs. In the spirit of “Parse, don’t validate” I might even be tempted to wrap it in some ValidatedPath type, but…
Agreed, again. Checks like this may make some kind of sense in some cases, though, for failing fast and/or in order to provide more meaningful error messages.
Right(path)
.filterOrElse(exists(_), "does not exist")
.filterOrElse(isDirectory(_), "not a directory")
.filterOrElse(isWritable, "is not writable")
.left.map(ErrorMessage)
Syntax highlighting in Discourse is crap for some reason.
Usually in markdown you can add scala after the first 3 backticks to select your language but Discourse either doesn’t know scala or just ignores the annotation. Or both.
I believe that’s a question of prefernces. I, on the other side, cannot stand the notation with the exclaimation mark and rather see a full comparison statement. But as I said, everyone is different.
I actually don’t see the point in returning anyhting else than Unit in this case, since nobody else is going to use the return value. But again, I have seen both and I for me the most important thing is to be consitend throughout a code base.
I did not know about the Either.cond, I would not have even thought about this. Thank you for pointing it out!
In the end both achieve the final result, I personally would not use for in this case; since, in general, I expect a for to flow most of the time instead of failing fast most of the time, also the for version requires more boilerplate. But, really use whatever makes more sense to you and your team.
I disagree, this is a common linting rule in all languages I know.