digitalmars.D - Feature request: clone a class (similar to typedef)
- coxalan (37/37) Sep 21 2007 Hello!
- Robert Fraser (2/59) Sep 21 2007
- BCS (4/7) Sep 21 2007 I run into it all the time when I want to create a new Exception type.
- Daniel Keep (6/16) Sep 21 2007 I feel your pain.
- Bruno Medeiros (13/70) Sep 21 2007 One alternative is to put the class's body into a template and then use
- Michael Kiermaier (6/63) Sep 22 2007 I have run several times into this situation (that I want a duplicate of...
- Bill Baxter (5/9) Sep 22 2007 You don't add keywords for things that have only come up "several
Hello! There are situations where you want an exact clone of a class, including two separated sets of static class members. A first try using typedef: import std.stdio; class foo { static uint a; } typedef foo foo1; typedef foo foo2; void main() { foo1.a = 1; foo2.a = 2; writefln(foo1.a); writefln(foo2.a); } gives the output 2 2 So typedef gives new types foo1 and foo2, but they share the same static class members. A version which distinguishes the two copies by a template id: import std.stdio; class foo(uint id) { static uint a; } void main() { foo!(1).a = 1; foo!(2).a = 2; writefln(foo!(1).a); writefln(foo!(2).a); } gives the output 1 2 So basically this works. But the problem is, that the code of the class needs to be extended by a id template variable. I wonder if it would make sense to introduce a new keyword, say 'clone', which works like 'typedef', but additionally gives a new set of static class members. coxalan
Sep 21 2007
coxalan Wrote:Hello! There are situations where you want an exact clone of a class, including two separated sets of static class members. A first try using typedef: import std.stdio; class foo { static uint a; } typedef foo foo1; typedef foo foo2; void main() { foo1.a = 1; foo2.a = 2; writefln(foo1.a); writefln(foo2.a); } gives the output 2 2 So typedef gives new types foo1 and foo2, but they share the same static class members.I've _never_ run into this situation, and I doubt it's a common one. Certainly too limited, IMO, for a language feature.A version which distinguishes the two copies by a template id: import std.stdio; class foo(uint id) { static uint a; } void main() { foo!(1).a = 1; foo!(2).a = 2; writefln(foo!(1).a); writefln(foo!(2).a); } gives the output 1 2 So basically this works. But the problem is, that the code of the class needs to be extended by a id template variable. I wonder if it would make sense to introduce a new keyword, say 'clone', which works like 'typedef', but additionally gives a new set of static class members. coxalan
Sep 21 2007
Robert Fraser wrote:I've _never_ run into this situation, and I doubt it's a common one. Certainly too limited, IMO, for a language feature.I run into it all the time when I want to create a new Exception type. I practically have "class NewExp:Exception{this(char[]c){super(c);}}" burned into my keyboard
Sep 21 2007
BCS wrote:Robert Fraser wrote:I feel your pain. import util.ex; // adds trailing Exception for me b/c I'm declared lazy ;) mixin NewException!("DivideByCucumber"); -- DanielI've _never_ run into this situation, and I doubt it's a common one. Certainly too limited, IMO, for a language feature.I run into it all the time when I want to create a new Exception type. I practically have "class NewExp:Exception{this(char[]c){super(c);}}" burned into my keyboard
Sep 21 2007
coxalan wrote:Hello! There are situations where you want an exact clone of a class, including two separated sets of static class members. A first try using typedef: import std.stdio; class foo { static uint a; } typedef foo foo1; typedef foo foo2; void main() { foo1.a = 1; foo2.a = 2; writefln(foo1.a); writefln(foo2.a); } gives the output 2 2 So typedef gives new types foo1 and foo2, but they share the same static class members. A version which distinguishes the two copies by a template id: import std.stdio; class foo(uint id) { static uint a; } void main() { foo!(1).a = 1; foo!(2).a = 2; writefln(foo!(1).a); writefln(foo!(2).a); } gives the output 1 2 So basically this works. But the problem is, that the code of the class needs to be extended by a id template variable. I wonder if it would make sense to introduce a new keyword, say 'clone', which works like 'typedef', but additionally gives a new set of static class members. coxalanOne alternative is to put the class's body into a template and then use template mixins to create several "clones": template Foo_body() { static uint a; int b; void someMethod() {} } class Foo1 { mixin Foo_body!(); } class Foo2 { mixin Foo_body!(); } -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Sep 21 2007
I have run several times into this situation (that I want a duplicate of a class). I like the proposal, in my opinion it would feel quite natural to have a third keyword besides alias and typedef. One is for aliasing a class (alias), one for duplicating the class type without the internals (typedef), and one for full duplicating (clone ore something different). Greetings, ~michael coxalan Wrote:Hello! There are situations where you want an exact clone of a class, including two separated sets of static class members. A first try using typedef: import std.stdio; class foo { static uint a; } typedef foo foo1; typedef foo foo2; void main() { foo1.a = 1; foo2.a = 2; writefln(foo1.a); writefln(foo2.a); } gives the output 2 2 So typedef gives new types foo1 and foo2, but they share the same static class members. A version which distinguishes the two copies by a template id: import std.stdio; class foo(uint id) { static uint a; } void main() { foo!(1).a = 1; foo!(2).a = 2; writefln(foo!(1).a); writefln(foo!(2).a); } gives the output 1 2 So basically this works. But the problem is, that the code of the class needs to be extended by a id template variable. I wonder if it would make sense to introduce a new keyword, say 'clone', which works like 'typedef', but additionally gives a new set of static class members. coxalan
Sep 22 2007
Michael Kiermaier wrote:I have run several times into this situation (that I want a duplicate of a class). I like the proposal, in my opinion it would feel quite natural to have a third keyword besides alias and typedef. One is for aliasing a class (alias), one for duplicating the class type without the internals (typedef), and one for full duplicating (clone ore something different).You don't add keywords for things that have only come up "several times". If you're not going to use it just about every time you touch your keyboard, then it shouldn't be a keyword. --bb
Sep 22 2007