Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
June 29, 2010 Class knowing its own Class | ||||
---|---|---|---|---|
| ||||
What is the pretty way to do something like this? Class C { private const char[] _name = "C";// demangling this.mangleof didn't work void makeNew() { mixin(`new `~_name~`();`); // the not so pretty part } } |
June 29, 2010 Re: Class knowing its own Class | ||||
---|---|---|---|---|
| ||||
Posted in reply to strtr | On Tue, 29 Jun 2010 17:59:37 -0400, strtr <strtr@sp.am> wrote:
> What is the pretty way to do something like this?
>
> Class C
> {
> void makeNew()
> {
> new typeof(this);
> }
> }
As edited...
-Steve
|
June 29, 2010 Re: Class knowing its own Class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | == Quote from Steven Schveighoffer (schveiguy@yahoo.com)'s article
> On Tue, 29 Jun 2010 17:59:37 -0400, strtr <strtr@sp.am> wrote:
> > What is the pretty way to do something like this?
> >
> > Class C
> > {
> > void makeNew()
> > {
> > new typeof(this);
> > }
> > }
> As edited...
> -Steve
Whahaha!
Thanks, I knew I was missing something here.
|
June 30, 2010 Re: Class knowing its own Class | ||||
---|---|---|---|---|
| ||||
Posted in reply to strtr | Dnia 30-06-2010 o 00:13:28 strtr <strtr@sp.am> napisał(a):
> == Quote from Steven Schveighoffer (schveiguy@yahoo.com)'s article
>> On Tue, 29 Jun 2010 17:59:37 -0400, strtr <strtr@sp.am> wrote:
>> > What is the pretty way to do something like this?
>> >
>> > Class C
>> > {
>> > void makeNew()
>> > {
>> > new typeof(this);
>> > }
>> > }
>> As edited...
>> -Steve
>
> Whahaha!
> Thanks, I knew I was missing something here.
Interestingly, this works even if makeNew is static (as factory methods usually are). Don't know whether by design or by bug. If by the latter, please don't fix it ;)
Tomek
|
July 01, 2010 Re: Class knowing its own Class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomek Sowiński | On Wed, 30 Jun 2010 22:59:50 +0200, Tomek Sowinski <just@ask.me> wrote: > > Dnia 30-06-2010 o 00:13:28 strtr <strtr@sp.am> napisal(a): > > > == Quote from Steven Schveighoffer (schveiguy@yahoo.com)'s article > >> On Tue, 29 Jun 2010 17:59:37 -0400, strtr <strtr@sp.am> wrote: > >> > What is the pretty way to do something like this? > >> > > >> > Class C > >> > { > >> > void makeNew() > >> > { > >> > new typeof(this); > >> > } > >> > } > >> As edited... > >> -Steve > > > > Whahaha! > > Thanks, I knew I was missing something here. > > Interestingly, this works even if makeNew is static (as factory methods usually are). Don't know whether by design or by bug. If by the latter, please don't fix it ;) > > > Tomek Yep, it's intentional. It's explained in a rather hard-to-find spot: "There are three special cases: 1. typeof(this) will generate the type of what this would be in a non- static member function, even if not in a member function. 2. Analogously, typeof(super) will generate the type of what super would be in a non-static member function. 3. typeof(return) will, when inside a function scope, give the return type of that function." http://www.digitalmars.com/d/2.0/declaration.html#Typeof |
Copyright © 1999-2021 by the D Language Foundation