Comments (5)
I thought about this. I wanted to leave the door open for future filesystems e.g. s3:///foo/bar
. More realistically, it could be a file inside a zipped file e.g. zip://foo.zip/!filename
etc. Those who expected the /home/user/test.txt
should call .pathAsString
to be more explicit anyway since .toString
of a file is not obvious what it should be by itself?
from better-files.
Check this out:
scala> case class Person(name: String)
defined class Person
scala> Person("Bob")
res0: Person = Person(Bob)
scala> res0.toString
res1: String = Person(Bob)
By, the above logic, the idiomatic .toString
for files should be s"File($path)"
e.g. File(/tmp/foo/bar)
. If that is true, then the uri version seems better to me.
from better-files.
Indeed case classes use different default toString, however to make b-f a convenient replacement for built-in java.io.File/java.nio.file.Path, b-f should mimic those where possible. So consider
scala> // Using "old" java.io.File API
scala> import java.io.File
import java.io.File
scala> val someFile = new File("foo")
someFile: java.io.File = foo
scala> val derivedFile = new File(someFile, "bar")
derivedFile: java.io.File = foo/bar
scala> // Using java.nio.file.Path API
scala> import java.nio.file._
import java.nio.file._
scala> val path = Paths.get("somthing")
path: java.nio.file.Path = somthing
scala> val childEl = path.resolve("else.txt")
childEl: java.nio.file.Path = somthing/else.txt
scala> // Using BF API
scala> val betterFile = better.files.File("foo")
betterFile: better.files.File = file:///Users/brandl/Dropbox/cluster_sync/scalautils/foo
scala> val betterDerivedFile = betterFile/"bar"
betterDerivedFile: better.files.File = file:///Users/brandl/Dropbox/cluster_sync/scalautils/foo/bar
Or just the code
// Using old java file API
import java.io.File
val someFile = new File("foo")
val derivedFile = new File(someFile, "bar")
// Using Path API
import java.nio.file._
val path = Paths.get("somthing")
val childPath = path.resolve("else.txt")
// Using BF API
val betterFile = better.files.File("foo")
val betterDerivedFile = betterFile/"bar"
b-f is neither using the same toString (which would be just the (optionally relative) path and not the uri) nor is it keeping path-relativity. Both are important features I think, which java-designers obvioulsy carefully preserved when rewriting File into Path API.
from better-files.
Although IMO, .toString
is meaningless since its just an arbitrary representation of an object and you should not trust or depend on it for anything except debugging purposes, but I will reopen this issue for the sake of meeting the expectations of users expecting the Java files behaviour
from better-files.
Codewise it should be easy to implement. It seems that reducing
val path = _path.normalize.toAbsolutePath
to
val path = _path
does the trick without breaking code or tests. The only other change would be to tweak the File.toString to use path.toString instead, which would require a single unit test (better/files/FileSpec.scala:71) to be adjusted.
File.pathAsString
could be removed since it would equal toString
A useful/necessary addition to better.files.File
would be
def absoluteFile = path.normalize.toAbsolutePath
to provide a means to derelativize a b.f.File instance (which is at the moment applied in the constructor but not always wanted/needed)
I've played around with such a modified version and it feels very good and consistent and indeed like a better java.jo.File/Path API.
I'd be happy to create a pull request but it's just changing a few words so it's barely worth the effort. :-)
from better-files.
Related Issues (20)
- make File serializible HOT 3
- Calling size on a broken symlink throws a NoSuchFileException,
- Switch to new codecov reporter
- Scala3 version HOT 6
- Support scala-native? HOT 2
- Use common settings style HOT 1
- Stream closed with Scala 2.12.15
- usingTemporaryDirectory should return `U` instead of `Unit` HOT 1
- is there a way to read huge gz csv file line by line of this package? HOT 1
- Make build pass on windows
- Make all side-effecty APIs use parens
- Add error handler to File.walk
- Imlement features from sbt IO
- Write self cleaning tests - no mutable vars
- Enable warnings for Scala 2.13 and above
- Generate scaladoc for all Scala versions
- Port ShapelessScannaerSpec to Scala 3
- scalaformat organize imports
- changeExtensionTo does not account for multiple dots in file name
- Directory Traversal Issue in File.unzipTo HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from better-files.