Namespace inside class #2112
Replies: 14 comments
-
Why not just use a nested class for this? |
Beta Was this translation helpful? Give feedback.
-
Uhh, I'm not sure I'm following. |
Beta Was this translation helpful? Give feedback.
-
How would you expect to call these methods? |
Beta Was this translation helpful? Give feedback.
-
@DanFTRX
The public struct ExperimentalFeatures
{
readonly AwesomeThing _obj;
// init _obj from ctor here
// public wrapper for the private method:
public void SomeCoolNewExperimentalThing() => _obj.SomeCoolNewExperimentalThing();
} The struct can see private members of the parent type. |
Beta Was this translation helpful? Give feedback.
-
@DanFTRX I'd expect it to work exactly like if you'd do it in the struct solution: |
Beta Was this translation helpful? Give feedback.
-
So just the same as a nested But also, this is inherently against how namespaces normally work. You seem to have a method directly in a namespace there - that's not legal C#. You'd have to have a nested class/struct inside, like this
This would have to be called as such |
Beta Was this translation helpful? Give feedback.
-
Since public class AwesomeThing
{
public void CommonlyUsedMethod() { }
}
namespace Experimental
{
public static class AwesomeThingExtensions
{
public void SomeCoolNewExperimentalThing(this AwesomeThing @this) { }
}
} You'd only get it to show up if you actually do |
Beta Was this translation helpful? Give feedback.
-
You could've made the code shorter by using expression body members. You also removed the constructor you would likely have anyways in the "after" portion. What I'm getting at is that you've skewed this in a way to prove a point. Regardless, this is literally the point of nested classes, is it not? |
Beta Was this translation helpful? Give feedback.
-
Going off of what @johnkellyoxford said class Thing
{
public void Blah() {}
namespace Foo
{
public static class Bar
{
public static void Blah2() {}
}
}
} Would have to be the final code, so why not just class Thing
{
public void Blah() {}
public static class Bar
{
public static void Blah2() {}
}
} Cut out the middle man? |
Beta Was this translation helpful? Give feedback.
-
How does Blah2 call any instance method on Thing? That's not what the original code does at all. |
Beta Was this translation helpful? Give feedback.
-
I think the percieved problem rikimaru wants to solve is that (s)he wants a way to group instance methods, possibly also grouping them in IDE auto-completes
[Edited: Updated list to reflect a few replies] |
Beta Was this translation helpful? Give feedback.
-
You can use a nested type and an instance property to emulate this kind of grouping: That gives you the good naming and access to the private state of the parent class: int x = foo.SomeApi(123);
int y = foo.Experimental.SomeExperimentalApi(456); |
Beta Was this translation helpful? Give feedback.
-
Seeing the list by @DanFTRX triggered a brainwave ... what about using an interface? public class AwesomeThing : AwesomeThing.IExperimentalFeatures
{
public void CommonlyUsedMethod() { }
void IExperimentalFeatures.SomeCoolNewExperimentalThing() { }
public IExperimentalFeatures ExperimentalFeatures => this;
public interface IExperimentalFeatures
{
void SomeCoolNewExperimentalThing();
}
} All of the methods are implemented directly on All experimental methods are publicly available by accessing |
Beta Was this translation helpful? Give feedback.
-
@HaloFour @theunrepentantgeek You guys suggested some awesome improvements, I'll use them. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Description
I want to be able to easily put stuff into its own namespaces inside a class.
Use Cases
Experimental or advanced features
...could be "hidden" behind an additional
.Experimental
so it doesn't pollute the original namespace with many things that might end up being scrapped. It also clearly signals that those things are not intended for production.Better segmentation
We already have partial classes for some rare cases where its unavoidable (code-generation tools; or large classes that simply cannot be split up for some reason).
Now imagine how clean some code/intellisense-lists could be if we could also put things into categories like that.
For example: many hardware emulators have pretty intricate code with some huge classes and breaking them up is not really feasible.
Most of the time each part of a partial class only references things in its own file/part and rarely needs to access the members of the other implementations.
Instead of getting many irrelevant things listed in intellisense it would only show the local "scope".
How it's done right now
What I want to be able to type
Implementation
There are two options:
Syntax Sugar
Auto generate the wrapper struct that we'd normally write ourselves.
Advantage: Since all it does is generating what's already possible there can't be any problems with it.
Disadvantage: Since declaring a struct-field in a type pretty much inlines the fields into the type, the type would still have a completely unneccesary reference to itself.
(yes, I know, in C# and IL the struct still exists, I'm talking about how it looks in memory when the process is running)
Make namespace part of the method-name
... which means that there's no hidden type and
SomeCoolNewExperimentalThing
would be a direct member ofAwesomeThing
.However the name of the method would literally be
ExperimenalFeatures.SomeCoolNewExperimentalThing
.Advantage: No hidden field!
Disadvantage: Is it even possible? Hopefully it wouldn't require any runtime changes.
Maybe the compiler can somehow work some of its witch-craft like it is already done for enumerators, async, and value tuples. 🤣
Potential problems:
Could be pretty wonky with reflection, maybe?
Actually, FindMethod etc should not really have any trouble with that.
But what about the runtime? Surely there's some code that looks up methods by name, but I doubt that anything like
methodName.Split('.')
is done (except probably in some badly written user-code)Beta Was this translation helpful? Give feedback.
All reactions