www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Singleton, alias to self?

reply Vladde Nordholm <v vladde.net> writes:
I'm not sure of how to use alias efficiently, so I want to know 
if I could somehow do this (psuedo-code)

class Singleton
{
   //So instead of calling `Singleton.getSingleton()` you just 
call `Singleton`
   alias this = getSingleon()

   //code for singleton...
}

Thanks in advance,
vladde
Feb 14 2016
next sibling parent reply Guillaume Piolat <name.lastname gmail.com> writes:
On Sunday, 14 February 2016 at 12:56:51 UTC, Vladde Nordholm 
wrote:
 I'm not sure of how to use alias efficiently, so I want to know 
 if I could somehow do this (psuedo-code)

 class Singleton
 {
   //So instead of calling `Singleton.getSingleton()` you just 
 call `Singleton`
   alias this = getSingleon()

   //code for singleton...
 }

 Thanks in advance,
 vladde
The "alias newThing = x;" syntax is not allowed with alias this. It's the only exception. You have to use alias getSingleton this;
Feb 14 2016
parent Guillaume Piolat <name.lastname gmail.com> writes:
On Sunday, 14 February 2016 at 13:23:28 UTC, Guillaume Piolat 
wrote:
 On Sunday, 14 February 2016 at 12:56:51 UTC, Vladde Nordholm 
 wrote:
 I'm not sure of how to use alias efficiently, so I want to 
 know if I could somehow do this (psuedo-code)

 class Singleton
 {
   //So instead of calling `Singleton.getSingleton()` you just 
 call `Singleton`
   alias this = getSingleon()

   //code for singleton...
 }

 Thanks in advance,
 vladde
The "alias newThing = x;" syntax is not allowed with alias this. It's the only exception. You have to use alias getSingleton this;
I've tried and fail to do what you want, even with a templated class + eponymous trick.
Feb 14 2016
prev sibling parent tcak <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
On Sunday, 14 February 2016 at 12:56:51 UTC, Vladde Nordholm 
wrote:
 I'm not sure of how to use alias efficiently, so I want to know 
 if I could somehow do this (psuedo-code)

 class Singleton
 {
   //So instead of calling `Singleton.getSingleton()` you just 
 call `Singleton`
   alias this = getSingleon()

   //code for singleton...
 }

 Thanks in advance,
 vladde
"this" is for an instance of class (or struct). There is no instance of define an alias at that time. I am not sure, but you can check "opCall" function as static for this. Give it a try at least.
Feb 14 2016