# Splitpath and joinpath should be reversible

**URL:** <https://discourse.julialang.org/t/splitpath-and-joinpath-should-be-reversible/44379>\
**Category:** General Usage\
**Created:** [August 6, 2020, 12:02am UTC](https://discourse.julialang.org/t/splitpath-and-joinpath-should-be-reversible/44379 "2020-08-06T00:02:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [August 6, 2020, 12:02am UTC](https://discourse.julialang.org/t/splitpath-and-joinpath-should-be-reversible/44379/1 "2020-08-06T00:02:36Z")

</div>

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

```julia
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 ?

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [August 6, 2020, 12:11am UTC](https://discourse.julialang.org/t/splitpath-and-joinpath-should-be-reversible/44379/2 "2020-08-06T00:11:12Z")

</div>

Perhaps it should.

However you can splat the vector of path components:

```julia
joinpath(x...)

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 6, 2020, 12:44am UTC](https://discourse.julialang.org/t/splitpath-and-joinpath-should-be-reversible/44379/3 "2020-08-06T00:44:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [August 6, 2020, 5:30am UTC](https://discourse.julialang.org/t/splitpath-and-joinpath-should-be-reversible/44379/4 "2020-08-06T05:30:51Z")

</div>

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 ? 😉

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [August 6, 2020, 5:50am UTC](https://discourse.julialang.org/t/splitpath-and-joinpath-should-be-reversible/44379/5 "2020-08-06T05:50:55Z")

</div>

> [@purplishrock](#):
>
> Maybe my first Julia PR ? 😉

Absolutely. Go for it!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 6, 2020, 10:21am UTC](https://discourse.julialang.org/t/splitpath-and-joinpath-should-be-reversible/44379/6 "2020-08-06T10:21:03Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 6, 2020, 1:54pm UTC](https://discourse.julialang.org/t/splitpath-and-joinpath-should-be-reversible/44379/7 "2020-08-06T13:54:15Z")

</div>

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.
