-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Path type #5550
Comments
So instead of:
I'll have to write:
and so on for every method? What about I don't oppose having a |
It would also be nice to know why the people that want |
As another note: ruby has a |
More questions: what about |
I like the idea of a |
I would go for class methods for all path-related methods that are currently on |
If we look at the average piece of path-handling code it rarely just does one operation in isolation so looking at For example, this code All those I'd go much further and remove most |
Most certainly it should just be a wrapper around String (plus an ivar for path type: posix/windows).
All methods receiving a path like
In the long term there should only be one way of doing path manipulation, that is using a It would already be an improvement to move all path-related class methods from |
I guess the only way to know is by actually implementing something like this and using it across the std and the compiler (probably even more also testing this on a few shards, maybe prax is a good one). I just worry about duplicating logic having to handle both String and Path across the std and Possible shards, plus having to wrap single calls like File.basenane into Path.new(...).basename |
If we go for a Dir.current #=> "/my/current/dir" (Path)
Dir.current[0..-2] #=> "/my/current" (String) == dirname
Dir.current[-1] #=> "dir" (String) == basename |
@asterite it's simple: we just make sure to call Plus, we can always go the whole way and just flat-out remove |
Most of the path-handling methods on |
I'm happy to start working on that. |
To make This approach will clearly separate two intentions:
What do you think? |
Here is a real life code snippet, to illustrate the previous post. There are 7 forced struct OVPN
getter path, ip, country_code, country_name
def initialize(@path : Path)
@ip = path.to_s[/(?:\d+\.){3}\d+/]
@country_code = (Geo.country_code_of(ip) || "UU")
@country_name = (Geo.country_name_of(ip) || "UNKNOWN")
end
def delete
File.delete @path.to_s
end
def move(to pool : Pool)
new_path = pool.path.join @path.basename
File.rename @path.to_s, new_path.to_s
end
def exchange_with(ovpn : OVPN)
return if @path.basename == ovpn.path.basename
File.rename @path.to_s, ovpn.path.parent.join(@path.basename).to_s
File.rename ovpn.path.to_s, @path.parent.join(ovpn.path.basename).to_s
end
end |
@sudo-nice There's no need for |
Personally i'd like to replace some of the class methods on |
@RX14 yes, it would be great to have something like these:
|
|
Yes, but it's not yet very well integrated into the stdlib. I think this issue should stay open for the time being. |
Also, what's the reasons why there some don't want FileSystem related method in |
This is obviously no a strict reason to keep |
I feel like this issue got pretty stale now. Maybe it would be better to open separate issues with concrete proposals on how to integrate it better? |
It has already been proposed in other discussions to add a
Path
type which essentially holds a filesystem path and turns current static methods ofFile
into an object oriented interface.The benefits of this approach are:
File
andDir
handle file system access,Path
performs only syntactic operations.Prominent implementations of this type are Java's
java.nio.file.Path
(API docs, tutorial) and Python'spathlib
(API docs).Pathlib actually comes in two flavours: pure paths provide only syntactical operations while conrete paths also do IO operations. IMHO it makes more sense to keep that separated like the Java approach.
Paths are implemented differently depending on operating system. Namely, we need to respect POSIX paths (like
/foo/bar/baz
) as well as windows paths (likeC:\foo\bar\baz
). The major difference are the primary directory separator (/
vs.\
) and designators for absolute paths (/foo
vsC:\foo
).Pythons
pathlib
offers two classes for this,PurePosixPath
andPureWindowsPath
, Java'sPath
by default use the variant supported by the default file system but you can use different providers (Path
is just an interface).I don't think this needs to be implemented in separate types but it would certainly be nice to not be limited to the system's native path model. This could be achieved by an ivar flag. But it's debateable if this is worth it, the most common use case by far will be working with native paths.
The following methods would migrate from
File
toPath
.Proposed additionals instance methods for
Path
:The type could probably also include
Enumerable(Path)
.All methods currently accepting a path as
String
would now expectPath | String
argument.The text was updated successfully, but these errors were encountered: