Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
motivation
Fixes github issue #58, to the extent that it Path.size will return a None if the length is 0. Semantically, it makes more sense to return a None for an empty file, rather than Some(0), since although a file was found, it was trivial.
issue #58 completeness
Issue #58 mentions that it's unclear whether exists works or not, which is not covered by this pull request. Probably, the issue should be closed, but a new issue should be opened to check that.
testing
I wasn't sure where to put a test in for this, but it should be tested.
different behavior
Previously, Path.size returned Some(0) for files that existed but were empty. Now, it will instead return None.
implementation choices
The existing implementation was a simple one-liner,
This was incorrect, but it was unclear how to fix it in a way that preserved its clarity and efficiency. The different ideas I toyed with were
This is the cleanest and the most straightforward, but I think that jfile.length() is not cached, and in the case where the file exists and is nonempty, will have to get a line length twice.
Another idea for how to implement it is to keep this clean system, but instead make it a block.
However, it requires using a block, which is unsightly.
Ultimately, I decided to go with something that looks ok, and that I guessed the compiler would probably optimize away anyway. The biggest concern was to bind jfile.length() to a variable so that I didn't have to call it twice, so I used a match expression, which lets me both check its value and binds its contents.
The value of the "if" part of the expression being a match expression is a little messy, which is distasteful, but I felt like adding another set of curly braces was worse.