Splitpath and joinpath should be reversible

Well, maybe “reversible” is not quite the right word.
I was suprised this doesn’t work.

julia> x=splitpath(raw"c:\foo\bar\baz")
4-element Array{String,1}:
 "c:\\"
 "foo"
 "bar"
 "baz"

julia> joinpath(x)
ERROR: MethodError: no method matching joinpath(::Array{String,1})
Closest candidates are:
  joinpath(::AbstractString) at path.jl:244
  joinpath(::AbstractString, ::AbstractString...) at path.jl:249
Stacktrace:
 [1] top-level scope at REPL[2]:1

julia> 

Shouldn’t it ?

1 Like

Perhaps it should.

However you can splat the vector of path components:

joinpath(x...)
3 Likes

Yes, I’ve wanted that method on more than one occasion.

2 Likes

very cool, i think i saw that at one point but had forgotten about it.

still, it’s very intuitive to expect joinpath to work on a vector of strings.

Maybe my first Julia PR ? :wink:

2 Likes

Absolutely. Go for it!

2 Likes

Ideally whatever works for vectors should work for iterable collections. Unfortunately, that conflicts with joinpath(::AbstractString).

I would recommend using reduce(joinpath, ...).

1 Like

One can define a joinpath(a) method with no type annotation and let the string-specialized methods handle the case where the argument is a string. Of course, totally unspecialized methods like that are a bit troublesome (and one of the big motivations for some kind of interface concept in the language so that we can dispatch on whether a thing is meant to iterate strings or not). But I think it would be fine.