How do I find a type conversion function? PosixPath -> String

I have been starting to use FilePathsBase.jl to manipulate paths, but its PosixPath type is not a subtype of AbstractString.

If I try to open ZipFile.Reader on a PosixPath, it fails because there is no version of ZipFile.Reader() that accepts a PosixPath. That’s a shame, but understandable.

What concerns me is I cannot find a function that converts a PosixPath to a String or other subtype of AbstractString.

I have tried and tried… I’m used to Smalltalk where I have system-wide introspection and inspection. Google doesn’t seem to be my friend.

Haskell has Hoogle which allows me to search Haskell libraries by type signature: FilePath → String

I’d like to search for something like PosixPath -> AbstractString which you’d reckon would give me a fighting chance.

The documentation of FilePathsBase says ‘1. Manually convert your path to a string before calling into the package.’ - without mentioning how to. So maybe it’s obvious to people who already know.

My specific questions:

  1. How do I find a function when I need one so obviously?
  2. What is the particular function I need for PosixPath → String ?

This appears to work:

julia> using FilePathsBase
julia> cwd()
p"C:/some/path"
julia> string(ans)
"C:\\some\\path"
julia> typeof(ans)
String
1 Like

I don’t know this package, but the first things I would try are String(path) and string(path), and see what comes out of it.

In general, if you want to convert value x to type T, you should try T(x) and convert(T, x). No idea if it will work in this case, though.

1 Like

Thanks very much - I tried String(path). I’m not sure of the idioms yet.
I still think we need joogle.julia.org did you look at the link for the search FilePath -> String on Hoogle? It makes so much sense with a good type system.

I’m sure a good search engine would very nice. But in this case you shouldn’t ask “How do I find out about operations mapping type A to type B?”, but “How do I convert a value to a type?” in which case the answer presents itself immediately: convert.

The above may not be completely clear. Try to think in terms of “what generic operation do I want to perform?” instead of “What can I do with this type?” The focus should be on (generic) functions and what they do, more than on the types they operate on. A shift of perspective, one might say.

1 Like

Thanks for your reply! A search would indeed be very nice.

convert presents itself to to those who already know. My question 1. was more general. Trial-and-error is not what I’m really asking for. Conversion is a special-case of the problem. I’m really hunting for a more general answer.

Looking at the output of my failed attempt at conversion. The compiler did a search, but only in my environment:

MethodError: no method matching String(::PosixPath)
Closest candidates are:
  String(::String) at boot.jl:350
  String(::Vector{UInt8}) at strings/string.jl:53
  String(::Symbol) at strings/string.jl:83
  ...

If it had looked in a registry (I think the term is), couldn’t there have been a better result? And… If I could have searched for PosixPath -> AbstractString would a such a specialized search be able to have found string(::PosixPath)?

Am I asking part 1 of my question in the right place?

I’m getting practical answers to my concrete problem - and can get on with my lowly hacking…

I’m also trying to make a more general point here, not just fix the concrete problem. I replied in the parallel thread, but, briefly, I’m suggesting a different approach to searching for solutions, based on what you want to do rather than what you want to do it to. In this particular case I made a guess, right or wrong, but I’m trying to be more general.

And, again, I’m not rejecting the search engine idea.

1 Like