@IluTov Your timing is impeccable. I was just in my IDE wishing for this. I'll take a read
So as a basic mechanism I think what you've got it an ideal first step, basic mangling entirely self contained. My particular use case is a bit more complex, I am not sure if "private" classes would be the right word for it, but "auto-sub-namespaced classes based on filename" doesn't exactly roll off the tongue.
@MarkR Right. Swift calls it fileprivate. I'd be happy with that if you are.
That said, another modifier begs the question: Do we add fileprivate to methods and so forth? It's potentially useless, but not nearly enough IMO. And then we have more modifiers that are only applicable in certain positions.
Well if we're talking purely about naming, in JS modules/TS everything is default internal unless given the export modifier, I would suggest something like 'local' but fileprivate would work too.
I suspect that once this lands and we get around to upgrading it, I'd try building a reflection hack where it parsed the mangled name, extracted the file path, and stored it relative to some root directory, so it could be re-created without caring which file path it was stored on (CI vs prod servers)
Nested classes might solve my use-case a little bit better tbh, although it'd be great if we could define them after the class rather than in it java style
Note that many programming languages allow nesting classes, but that doesn't necessarily make them private. E.g. it can just alter their namespace. E.g. a Kind enum in a Ast.Node class might become Ast.Node.Kind, which is still usable from the outside. So it might have to be combined with private.
And as Tim mentioned, in PHP, this comes with some autoloading issues if the nested class is not actually private.
@MarkR Yes and no. It could be fixed in two primary ways, scanning the entire project and building a map, or recursively looking at the parent of the namespace (e.g. Ast\Node\Kind first, then Ast\Node, then Ast. But both come with some limitations.
Hmmm yeah it treating the string exactly like the class would throw a wrench into it. There's some options, but I'd think it best to stay away from re-using the namespace separator
I get where you're coming from it, and I see the distinct similarities between a namespace-namespace and class-contained namespace, but like you say it would cause issues with autoloading, especially as you'd then have 2 (or more depending on level of nesting) valid locations which is a tad odd.
That said, recursive loading of classes should be quite rare, and especially deeply nested ones. So in practice it might actually be ok. Maybe something to discuss with the Composer folks. This would open up the possibility for other things, such as sealed classes, or possibly simplify the design of ADTs.
In particular, enum Option { case None; case Some($value); } could result in a class Option\Some, which would simplify matching, avoiding the necessity for pattern matching just to make this useful. In theory, we could even just use its constructor, i.e. new Option\Some(), which can also be imported, so shortened to new Some().
I just realized fileprivate is wrong anyway. It's not just restricted to the current file, but also the current namesapce block. It's been a few months since I implemented this. If we consider a class a new namespace, private on a nested class would work in a logically consistent manner.
@IluTov I don't fully understand the argumentation with nested classes in the RFC - for namespace visibility it does nothing. But what's the argumentation why file private classes should not just be replaced by nested classes?
'Just' is perhaps doing a lot in that statement as I would assume internal classes would be a much bigger project. I can see the value to private auto-mangled symbols, but internal classes + visibility modifiers on them are undoubtably more powerful (and would meet my use cases far better tbh)
@bwoebi They are two separate features. You could argue standalone private classes shouldn't be allowed, but what's the point? Especially if standalone private functions are allowed.