Replies: 7 comments
-
I've wanted this so bad! |
Beta Was this translation helpful? Give feedback.
-
I really like this. Many projects I see have a noise of classes that should be private to other classes, but are instead littering a namespace because someone didn't want to deal with a mountain of indents. These are typically |
Beta Was this translation helpful? Give feedback.
-
As just syntactic sugar, I can see the merit of this. But on the flipside, it also comes with the weirdness that it would introduce a disconnect between the scope as a human may interpret it, and the scope as the compiler would interpret it: public class Foo : IFoo
{
// Some common logic for all nested types
private static int SharedHelperMethod() => 42;
public IBar Bar { get; } = new Bar();
}
private class Foo.Bar : IBar
{
// Would have to be legal
public int M() => SharedHelperMethod();
} Someone just skimming through source code (especially outside of an IDE) might miss the But then again, education is part of every new feature. |
Beta Was this translation helpful? Give feedback.
-
@Joe4evr That's really not different from if |
Beta Was this translation helpful? Give feedback.
-
@jnm2 Maybe, but it's a lot different when there's actually a lot of logic involved and it's not part of an interface contract. 😉 |
Beta Was this translation helpful? Give feedback.
-
I wouldn't underestimate the value of eliminating extraneous indentation. For C++, people have complained for a long time at not being able to do
I really don't think so. This shouldn't be any more difficult to mentally parse than the similar syntax for nested namespaces. It is already common for nested classes to be in a separate file, so I wouldn't expect noticing the class is nested via indentation and extra curly braces vs. a concise dot syntax to be much different. |
Beta Was this translation helpful? Give feedback.
-
I may add that if the container class doesn't exsist, its name is conseders as a namespace:
If there is no Outer class, then this translated to:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Porting this from dotnet/roslyn#1537
I can define a namespace as:
or as
Either way, I refer to that namespace as "Foo.Bar".
I'd love the same flexibility for nested classes. That is, today I can write:
but I'd really like to be able to write:
Where "partial" is implied by the concise class definition syntax.
The scenario where I would find this particularly useful is when a set of classes implement a set of interfaces, but the implementation of certain interfaces is an implementation detail of the implementation of a higher level interface (and therefore a nested class as it does not need to be publicly exposed but may need private access to the parent class). For example:
This level of nested classes is a perfectly reasonable design for this type of situation, and results in the right level of encapsulation, but today is syntactically awkward as it ends up producing overly indented code.
Beta Was this translation helpful? Give feedback.
All reactions