Pseudo namespaces
Jonathan M Davis via Digitalmars-d
digitalmars-d at puremagic.com
Thu Dec 3 15:23:01 PST 2015
On Thursday, 3 December 2015 at 22:27:45 UTC, Andrei Alexandrescu
wrote:
> On 12/03/2015 05:14 PM, Jonathan M Davis wrote:
>> You declare static functions on a struct or class and then
>> make the
>> struct or class unusable as an object (e.g. by having a final
>> abstract
>> class or explicitly disabling all ways to construct the struct
>> or class).
>
> I must be dense. Consider:
>
> struct List
> {
> int x;
> template _stable()
> {
> void fun()
> {
> import std.stdio;
> writeln(this.x);
> }
> }
> alias stable = _stable!();
> }
>
> void main()
> {
> List lst;
> lst.stable.fun();
> }
>
> Could you please redo the example with a struct?
Sorry, but I clearly didn't pay enough attention to what you were
doing, because I only saw the namespace that you put at the
module level and not the one inside of the struct. The one at the
module level works just fine with a struct or class, avoids
having to declare a alias, and allows you to just shove the
documentation on the functions normally without having to explain
that the functions in _MyNamespace need to be called with
MyNamespace instead. It would also work inside of a class or
struct except that based on the implementation of fun, you want
access to the an object of the struct or class that it's in, and
I don't see a way to do that anymore than you do.
That being said, I confess that I don't see any reason to create
namespaces inside of a struct or class. They already have to be
used as part of the struct or class, so they're namespaced at
that level already, and if you have so many member functions that
you feel the need to namespace them further, then that implies
that you have way too many IMHO. But you can always just put the
"namespace" in their names. It's even one character shorter,
because you avoid the period.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list