What makes this different to not overloading + for Strings, which seems to have been shot down multiple times (including just before Rust 1.0, I believe)? For the record, I do like this idea though.
I'm not sure this would work well in practice - for example, having a PathBuf called tmp for a temporary file seems pretty reasonable.
This exact thing was just being discussed in this reddit thread. My conclusion is that some people are just picky about what ops overloads should be valid.
Two valid critique I saw in a linked RFC for a new Convert op were here and here. Other valid alternatives would be having a dedicated concat operator which would probably replace both + and this proposed / (it would also avoid the controversy these overloads bring).
Problematically, a concat operator doesn’t seem to be universally popular. Some don’t see the difference between + and concat. Others don’t want an extra operator (maybe it’s not important enough). Also += overloading seems to be another alternative for some reason but I don’t really see how it’s any different from +.
Misusing placement new is an advanced kind of evil, which we should forgo if a more primitive evil (misusing Div) suffices.
Also I have yet to see a filesystem where <- denotes the path separator, whereas / is common on Unices and Windows’ \ (which is already used for both string escapes and line continuation) looks similar enough. Also both Python and Java accept / instead of \ even on Windows.
I would expect every addition operation to be commutative, i.e. "asd" + "fgh" should be equivalent to "fgh" + "asd". This obviously doesn't work for strings and lists (but does for sets).
I'm not saying that this must hold for programming languages, but it might be nice. (Division for paths works surprisingly well from a mathematical standpoint.)
In my opinion, the real difference is that we see a lot more code that mixes string handling and arithmetic, whereas this is much less common with path handling – e.g. how often do you see code like prefix + (x + y).to_string() + suffix vs. code like prepath / &(a / b ).to_string() / sufpath`?
That is quite nice, but I think this would require RHS = Self? Given that Add can be implemented otherwise, I'm not sure this is the best guideline (and isn't backwards-compatible, because there's a stable Add<&str> for String implementation).
I’ve just tried to naïvely implement this in a crate, but run against the orphan rule. So to build this, I’d need to wrap std::path::Path in a struct ext::path::Path (that has much of the same interface, but also implements Div).
Also PathBuf. Even then, it wouldn’t be compatible with std::path::Path. So this exercise would be rather futile.
Personally I’m very hesitant when it comes to overloading operators for anything, but their original purpose. I particularly dislike abusing operator overloading to build mini DSLs, which I’d argue this is a case of.
Furthermore, if we are doing such overloading, I find it hard to argue for having different operators to append data, depending on the type. I can sort of get on board with “Adding/appending a string to another one”, and the operator being + (i.e. addition). However, why is “Adding/appending a path to another one” represented by a different operator / (i.e. division) instead? The answer is obviously that this looks familiar, but from a completely different context than Rust programming.
I’d prefer if we encapsulated such DSLs within macros, e.g.: