www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Non continual namespaces?

reply "Joey Peters" <squirrel nidhogg.com> writes:
Sometimes I want to group something further than you can, like in classes. 
What I would like is something like:

class Object {
 int data;
 typeof(this) modify() { data += 1; return this; }
 namespace copy {
  typeof(this) modify() {
   return this.dup.objectModifyingRoutine();
  }
 }
}
Object myObject = new Object;
myObject.modify();
Object secondObject = myObject.copy.modify();

The namespace should still be in the scope of the class; so it can access 
it's (though lower level) members. This would be useful in some cases. 
Currently I'm doing it with classes that have a reference to Object.
Mar 21 2005
next sibling parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
"Joey Peters" <squirrel nidhogg.com> wrote in message 
news:d1m764$2kre$1 digitaldaemon.com...
 Sometimes I want to group something further than you can, like in classes. 
 What I would like is something like:

 class Object {
 int data;
 typeof(this) modify() { data += 1; return this; }
 namespace copy {
  typeof(this) modify() {
   return this.dup.objectModifyingRoutine();
  }
 }
 }
 Object myObject = new Object;
 myObject.modify();
 Object secondObject = myObject.copy.modify();

 The namespace should still be in the scope of the class; so it can access 
 it's (though lower level) members. This would be useful in some cases. 
 Currently I'm doing it with classes that have a reference to Object.

I'm curious what else is going on in your class. Based what you've pasted above I'd recommend exposing the "dup" property and having the user call that instead of using a "copy" namespace: class Object { int data; typeof(this) modify() { data += 1; return this; } Object dup(); // abstract? typeof(this) objectModifyingRoutine() { // how does this differ from modify? } } Object myObject = new Object; myObject.modify(); Object secondObject = myObject.dup.modify(); Note calling a user-class Object will be confusing for user (and is it even allowed?) so I'd also recommend choosing another name for your class.
Mar 21 2005
parent "Joey Peters" <squirrel nidhogg.com> writes:
"Ben Hinkle" <bhinkle mathworks.com> schreef in bericht 
news:d1n4ud$gno$1 digitaldaemon.com...
 "Joey Peters" <squirrel nidhogg.com> wrote in message 
 news:d1m764$2kre$1 digitaldaemon.com...
 Sometimes I want to group something further than you can, like in 
 classes. What I would like is something like:

 class Object {
 int data;
 typeof(this) modify() { data += 1; return this; }
 namespace copy {
  typeof(this) modify() {
   return this.dup.objectModifyingRoutine();
  }
 }
 }
 Object myObject = new Object;
 myObject.modify();
 Object secondObject = myObject.copy.modify();

 The namespace should still be in the scope of the class; so it can access 
 it's (though lower level) members. This would be useful in some cases. 
 Currently I'm doing it with classes that have a reference to Object.

I'm curious what else is going on in your class. Based what you've pasted above I'd recommend exposing the "dup" property and having the user call that instead of using a "copy" namespace: class Object { int data; typeof(this) modify() { data += 1; return this; } Object dup(); // abstract? typeof(this) objectModifyingRoutine() { // how does this differ from modify? } } Object myObject = new Object; myObject.modify(); Object secondObject = myObject.dup.modify(); Note calling a user-class Object will be confusing for user (and is it even allowed?) so I'd also recommend choosing another name for your class.

The class Object is just in the example. Maybe it would be more clear with string modifying routines, or whatever. The point is I want to group one level further without using a member of a new class type that holds a reference to the original class. Is that a no go?
Mar 22 2005
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Joey Peters" <squirrel nidhogg.com> wrote in message 
news:d1m764$2kre$1 digitaldaemon.com...
 Sometimes I want to group something further than you can, like in classes.

I would like to do that as well. Up until 0.119, I was able to use structs to accomplish this sort of "namespace in a class." That's no longer available.. Though it'd be great to have this kind of functionality to, for example, group functions that perform a similar duty together in a class. I doubt it'd be very difficult to implement - it'd just involve tacking on the namespace name to the front of the enclosed symbols. And to extend it further - abstract classes whose methods and variables are all static (i.e. singletons) could be replaced with a module-level namespace. So instead of writing: module something; abstract class A { public: static int x() { return _x; } private: int _x; } Could be replaced with: module something: namespace A { public: static int x() { return _x; } private: int _x; } Any file importing the module would not have to change naming - A.x() would still be called A.x(). This could also be used, for nothing else, to enforce the use of explicit name qualification (i.e. rather than importing std.string and being able to call toString, could enforce the use of the full std.string.toString name).
Mar 22 2005
parent reply "Joey Peters" <squirrel nidhogg.com> writes:
I used structs as well. Right now I'm using template mixins with an added 
identifyer:

#class Foo {
#  void a() { }
#  template _Copy() {
#    void a() { }
#  }
#  mixin _Copy copy;
#}
#(new Foo).copy.a();

"Jarrett Billingsley" <kb3ctd2 yahoo.com> schreef in bericht 
news:d1qmsk$16ks$1 digitaldaemon.com...
 "Joey Peters" <squirrel nidhogg.com> wrote in message 
 news:d1m764$2kre$1 digitaldaemon.com...
 Sometimes I want to group something further than you can, like in 
 classes.

I would like to do that as well. Up until 0.119, I was able to use structs to accomplish this sort of "namespace in a class." That's no longer available.. Though it'd be great to have this kind of functionality to, for example, group functions that perform a similar duty together in a class. I doubt it'd be very difficult to implement - it'd just involve tacking on the namespace name to the front of the enclosed symbols. And to extend it further - abstract classes whose methods and variables are all static (i.e. singletons) could be replaced with a module-level namespace. So instead of writing: module something; abstract class A { public: static int x() { return _x; } private: int _x; } Could be replaced with: module something: namespace A { public: static int x() { return _x; } private: int _x; } Any file importing the module would not have to change naming - A.x() would still be called A.x(). This could also be used, for nothing else, to enforce the use of explicit name qualification (i.e. rather than importing std.string and being able to call toString, could enforce the use of the full std.string.toString name).

Mar 23 2005
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Joey Peters" <squirrel nidhogg.com> wrote in message 
news:d1rfl7$20qo$1 digitaldaemon.com...
I used structs as well. Right now I'm using template mixins with an added 
identifyer:

Ooooooooooooooooh! :)
Mar 23 2005
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
news:d1sm9m$95n$1 digitaldaemon.com...
 Ooooooooooooooooh! :)

Sadly, in attempting to implement this in my code, I've come across both what seems to be a bug, and a shortcoming. I've posted the bug on the bugs board, and the shortcoming is a little complicated. I have two property sets, each of which have functions of the same name. In a single class, that's not a problem; the mixin namespace disambiguates the names. But if I try to derive from that class and override the functions which are defined in the template, the compiler doesn't seem to be able to disambiguate the names. It thinks I'm trying to override a single function in the original class twice, as the mixed-in functions are sort of in their own namespace, but sort of not.
Mar 23 2005