Feature request: clone a class (similar to typedef)

Michael Kiermaier michael.kiermaier at gmx.net
Sat Sep 22 00:47:56 PDT 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




More information about the Digitalmars-d mailing list