Giter Site home page Giter Site logo

Comments (5)

pathikrit avatar pathikrit commented on June 14, 2024

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.

pathikrit avatar pathikrit commented on June 14, 2024

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.

holgerbrandl avatar holgerbrandl commented on June 14, 2024

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.

pathikrit avatar pathikrit commented on June 14, 2024

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.

holgerbrandl avatar holgerbrandl commented on June 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.