On 05/03/2025 23:11, Rob Landers wrote:

I'd like to introduce my RFC for discussion: https://wiki.php.net/rfc/short-and-inner-classes


As a user, I find the concept of inner classes quite confusing.

However, I was looking at some code earlier and thought an "inner enum" would be useful, to replace various "MODE_FOO" type class constants - but I see those are left to future scope. :(


I'm aware they exist in a lot of other languages, though, so I thought I'd look around at how the proposed version compares. It seems there's quite a zoo out there...

One common theme I do note is that all four of the pages I found are titled "nested classes" or "nested types"; in some cases, "inner class"/"inner type" has a specific meaning, which I don't think matches the RFC's proposal.


C# - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/nested-types

I think this is most similar to what you're proposing. Interestingly, the nested class defaults to being private, i.e. only usable within the parent class.

Classes, structs and interfaces can all be nested inside each other.


Java - https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

There are two types of nested class (plus some more related concepts which seem less relevant)

- "Static nested classes" - static in the sense that they act like a static member of the surrounding class; appear to be *only* about namespacing and visibility of the class, with no special access to the surrounding scope - "Inner classes" - associated not just with a parent class, but a parent *instance*, so have to be created with the syntax "ParentClassName.new InnerClassName" or "this.new InnerClassName"; are not allowed to have static members


Kotlin - https://kotlinlang.org/docs/nested-classes.html

Just nesting a class gives it no special access, but the keyword "inner" causes it to carry an *instance* of the outer class, which can be explicitly referenced with the syntax "this@OuterClassName".

Classes and interfaces can be nested in any combination.


Swift - https://docs.swift.org/swift-book/documentation/the-swift-programming-language/nestedtypes

Type declarations of various sorts can be nested, and have all the visibility modifiers available to other declarations.

"Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file" - https://docs.swift.org/swift-book/documentation/the-swift-programming-language/accesscontrol/

The example of a nested enum also demonstrates a nice shorthand syntax, where the ".ace" in "BlackjackCard(rank: .ace, suit: .spades)" is short for BlackjackCard.Rank.ace, inferred from the parameter type.


I don't have any specific conclusions, but I think with features like this it's always worth examining other people's ideas, to see if we want to include (or avoid) any of them.


--
Rowan Tommins
[IMSoP]

Reply via email to