Suggestion: Path construction operator for File


How about adding a new operator “/” to File and String? It would allow for lovely simple cross-platform path construction:

File myFile = File::getSpecialLocation (File::userApplicationDataDirectory) / "MyFolder" / "Preferences" / "BaseConfiguration.prefs";
It should work fine with String, File and possibly also other non-math objects. Apart the improved readability, it is also very comfortable for programatically constructing and traversing directory structures :smiley:

What do you think?


You know you can use “/” in all platform (it’s supported in Windows), so the previous code would look like:
File myFile = File::getSpecialLocation (File::userApplicationDataDirectory).getChildFile(“MyFolder/Preferences/BaseConfiguration.prefs”);

Overloading operators are a good idea when it’s obvious to do so. I’m not sure using “/” as an operator is so obvious for a File object (and it’s absolutely not for const char * like in your example, so the second operator “/” in the previous example will lead to “can’t divide two pointer” error)


Yeah, operator overloads that don’t behave like normal numeric types are best avoided. It’s a cute idea, but as cyril said, it actually works out to be more efficient if you just put the slashes into the string.


Oops, I didn’t think of const char* being out of reach here. I am still struggling a bit with a world that does not consist solely of objects.

Nice to know that “/” also works on Windows, I didn’t ever use it there. I would suspect it might cause a lot of trouble, for example when spawing child processes, opening documents, writing to the registry, or passing file names to command line tools. I would not rely on it.

Of course, writing a full path straight into the source code is trivial, but I was rather thinking about traversing (or creating) directory structures and composing paths from components. This currently is very verbose and complicated, IMO. I would at least expect a function like

const File File::addComponent (const String component)

or something similar that adds a subdirectory or a filename to an existing File. This could be used to recursively build paths.


getChildFile() ?


Ouch. I shouldn’t be working until 3:00 AM in the morging that often anymore. :oops:

The naming of that function is a bit misleading, though. The prefix “get” does not suggest that something is added to the filename. That’s why I didn’t recognize it as such.

Something that initially confused me with Juce is that it does not conceptually distinguish between files and filenames. Actually these are quite different things. I first felt reluctant to adding a File as a member to my class, because I thought “hey, a file might keep OS handles and buffers and whatnot around, I don’t want to add all that to my class.” Until I realized that File basically is only the filename with a bunch of convenience methods added.


It “gets” a new filename for you, by adding the path and returning the result. If it was called something like “addComponent”, that’d make it sound like it was a non-const modifying operation.


Ok, I see the point. Still, the name getChildFile() suggests that it accesses something that already exists, possibly failing when the child doesn’t exist.

Anyway, good to know the needed function is there to use :slight_smile: