Won’t it be useful to always append trailing slashes to paths for all core path functions? We then would see at first glance if a path leads to a directory or a file. When you see for example ~/config, you can’t tell if this is a file or a directory – the implementation of this convention would clarify this.
Some additional thoughts:
Currently we have both, as normpath(@__FILE__, "..") shows a trailing slash, whereas normpath(@__DIR) (or just @__DIR__) show no trailing slash. This could be unified.
dir * "/" * file would then expand to "<DIR>//<FILE>". That doubled slash makes no difference, which makes this backwards-compatible.
It should be better for Windows compatibility, as I assume dir * "/" * file wouldn’t work there, whereas dir * file (with trailing slash convention) should do the trick, as this should automatically use a backslash.
dir * "/" * "var" could expand to /var if dir is empty – which could have negative impact on system roots.
“You should do it right” solves every problem on earth, hooray!
I know about joinpath(), still a LOT of people (especially beginners) use … * "/" * …. Everything can be further polished, and I think that this would make things clearer and improves some situation for beginners without introducing negative consequneces. If you are against, then please describe at least a downside argument.
I don’t know if this is true, but if anyone is handling paths like this, then they shouldn’t. This means that your last 3 examples are not very relevant.
Paths have existence independently of the file system. You cannot tell if "/some/path" is a file, directory, or neither, until you examine it (eg isdir), except for some cases (isdirpath).
They should also not do memory bugs in C and null pointer exceptions in Java…
An example – I just stumbled upon this thread here:
I know this is not necessary, but it would make Julia more nice, see this screen shot of my Juno session:
Is it a folder or a file? With normpath() adding a trailing slash, you would be able to infer that! Yeah, isfile() is 100% precise, but here a trailing slash just makes it more comfortable and you get maybe 90% precision instead of currently none at all. And again, I don’t see a reason not to do that.
For what it’s worth, neither Python, Ruby nor bash append the trailing /. (Well, for bash it of course depends a bit on the context, but for example pwd doesn’t append.) If I had to take a guess I’d say it’s probably very standard not to append it.
I’m not necessarily arguing Julia should follow precedents set by those languages (in many cases it does not for good reason and the result is wonderful), but I don’t personally see any compelling reason to buck the trend here, particularly when Julia has a pretty extensive set of file management functionality in Base.
If anything, it might be worth investigating why normpath(dir,"..") appends trailing slash, as that would seem to be the non-standard thing here. Even weirder is that it only seems to do that when you use "..". It doesn’t seem like a big deal, but it’s always nice to have predictable behavior.
For bash, it would definitely be very bad practice to append a / automatically, since many command line tools (eg cp and rsync) do something completely different depending on whether you append one.
FWIW, I think that conditioning on a trailing / is an unfortunate design choice (and consequently a source of many bugs), but it looks like it is established practice, so I am not sure Julia would benefit from automatically appending a / either — it would make run(`something $(withpath)`) tricky.
I guess it might have been nice if dir/ were standard notation for a directory to denote that fact that it’s a directory. We’re 40 years too late on that decision though
Wouldn’t mv <file> <dir> in a shell actually be better and safer when dir contains a trailing slash? This way you cannot as easily accidentally mix files with folders in commands, so that should be more precise and a bit like type-safety…? When you want to move something in a directory and use a path without a trailing slash, mv would think that this is a file instead, or? (Sorry, I’m not really into shell scripting…)
Does anybody think a Path or File or Dir type would be useful?
I’ve had quite a few instances where I wanted methods in the line of: load_data(file_name::String) load_data(data::String)
When dealing with these I had to come up with artificial solutions when what I really would’ve wanted was to dispatch on a Path type (or File or Dir): load_data(file_name::File)
This would also address the “is it a file or a dir” question and would have distinct show, print etc functions which could take into account end slashes, extensions, etc.
There was also a work-in-progress Julep for this idea: https://github.com/JuliaLang/Juleps/pull/26. Didn’t get far enough to make it into Julia 1.0 but still an interesting idea that would be worthy of someone exploring fully to see how nice one can make working with paths with this approach.
I am not saying that the interface to mv etc is well-designed (quite the opposite), but at this point some tools acting differently depending on the trailing / is pretty much a given for Julia.