Coder Social home page Coder Social logo

Comments (5)

kylef avatar kylef commented on August 15, 2024

This is an interesting idea, but there is no way to guarantee your path will still exist and if it is a directory or regular file for the life-span of your instance of the Path.

Example:

let myDirectory: DirectoryPath = Path("x").directory()

// Somewhere in a different process, application or thread:
// `rm x`, `echo "content" > x`, etc

try myDirectory.children()  // fails because x is no longer a directory but a regular file

Therefore there isn't really any added safety. Splitting them into protocols may make sense, we could have DirectoryPath RegularFilePath which Path conforms to. RegularFilePath can include the methods related to reading and writing. DirectoryPath can contain the methods for viewing the children and mkdir, chdir.

/cc @mrackwitz do yo have any opinions with this?

from pathkit.

kylef avatar kylef commented on August 15, 2024

The protocols would allow you to indicate that your function may accept or return a particular path of a specific type.

Example:

func parseJSON(file: RegularFilePath) -> AnyObject {}

But of course, this doesn't really force given value to be a regular file path since it the underlying file on disk may change.

from pathkit.

kareman avatar kareman commented on August 15, 2024

Sure there is always the possibility items in the file system will change and make your paths invalid, but that problem will always be there. What I meant by catching more bugs at compile time is that it would be impossible to perform file operations on a path the programmer has designated as a directory path:

let myDirectory = DirectoryPath("x")

// won't even compile
myDirectory.write("Hello World!")

But using protocols instead should work too, though instantiating paths would be a bit more cumbersome:

let myDirectory = Path("x") as DirectoryPath
// vs
let myDirectory = DirectoryPath("x")

from pathkit.

mrackwitz avatar mrackwitz commented on August 15, 2024

I was thinking about similar ideas using protocols and default implementations.
You could also extend that to nearly all checks which need to be done.

protocol ExistingPath : Path {
    func delete() {  }
}

protocol ReadablePath : ExistingPath, RegularFilePath {
    func read() -> NSData {  }
}

This could be used with Swift's optional unwrapping:

if let existing = file.existing {
    existing.delete()
}

But it could also enhance the above example to something like:

func parseJSON(file: ReadablePath) -> AnyObject {}

But where would you draw the line?

I'm not convinced that Swift is really the right language to do something like that. A language with dependent types would naturally hosts concepts like that. But it would be still really fallacious safety as anything could change anytime on disk as @kylef already explains.

from pathkit.

kareman avatar kareman commented on August 15, 2024

note: I make no claims about improving filesystem access safety. Just trying to catch more programmer errors at compile time.

@mrackwitz I've been thinking along the same lines. I think of a path as an address to a filesystem item which may or may not currently exist. So in order to use a path to access a file or directory you should first have to check if it exists. Like in the if let existing = file.existing example above. But where indeed do we draw the line?

How about at 4: DirectoryPath and FilePath, Directory and File, where the last 2 are reference types ( objects) referring to actual items in the filesystem. If those items are no longer accessible when we try to perform operations on them we will find out in the errors that are thrown.

from pathkit.

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.