Comments (5)
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.
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.
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.
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.
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)
- Swift 3 support HOT 7
- PathKit wont compile with swift 3.0 snapshot DEVELOPMENT-SNAPSHOT-2016-05-03-a HOT 1
- Swift Package Manager requires Package.swift HOT 2
- Support for Carthage dropped? HOT 6
- Bump the version to 0.7.1 HOT 4
- I want to get the size of the folder, I found this method < Path.filesize >, but I find that the size of the inaccurate, where is the problem HOT 1
- Going from absolute path to relative? HOT 5
- Combining two pathes including step back lead to incorrect path
- New Tag for SPM 4.0 HOT 5
- Glob function issue HOT 4
- Status of Linux support HOT 10
- Future of Project HOT 3
- Support recursive glob '**' HOT 1
- Why use NSString? HOT 3
- Unable to build with Xcode 13 GM HOT 10
- Xcode 13 compiler error HOT 4
- Glob function on windows HOT 2
- PathKit method calls HOT 1
- It has problem when used in App(not SPM or Terminal)
- How to add text to the last line of a text file
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 pathkit.