digitalmars.D.learn - standard alias for a class name inside the class code?
- dan (21/21) May 28 2016 Is there a standard alias for a class name inside class code?
- Mithun Hunsur (2/6) May 28 2016 typeof(this) gets you the type of the current class. :)
Is there a standard alias for a class name inside class code? Something like 'this' referring to a class instance, but referring instead to the class itself? What i would like to do is have something like class Clas { // alias Clas THIS; <- don't want this boilerplate static THIS make_instance( .... ) { auto x = new THIS( ); .... return x; } } This would be great for copy/paste, changing class names, and in general communicating your intention. I'm guessing the answer is no, and that there's some compelling reason why a compiled language wouldn't want to provide this feature. (But the php interpreter, whatever else is good or bad about it, does let you write 'new self(...)' and does the right thing with it.) TIA for any clues. dan
May 28 2016
On Sunday, 29 May 2016 at 00:14:17 UTC, dan wrote:Is there a standard alias for a class name inside class code? Something like 'this' referring to a class instance, but referring instead to the class itself? [...]typeof(this) gets you the type of the current class. :)
May 28 2016
On Sunday, 29 May 2016 at 00:28:13 UTC, Mithun Hunsur wrote:On Sunday, 29 May 2016 at 00:14:17 UTC, dan wrote:Great!! Thanks Mithun! That certainly works. But i sure don't understand how. Especially in a declaration like static typeof(this) make_instance( ) but also in the 'new typeof(this)'. In both cases, 'this' doesn't even exist. In fact, if you make a reference to this inside the static function make_instance(), you get an error: 'this' is only defined in non-static member functions, not make_instance So the compiler itself states that 'this' is not defined. But nevertheless, your method absolutely does work. So i suppose i should not look a gift horse in the mouth, but i'm still puzzled. Anyhow, thanks a million, because whether or not i understand your idiom, it is exactly what i need. danIs there a standard alias for a class name inside class code? Something like 'this' referring to a class instance, but referring instead to the class itself? [...]typeof(this) gets you the type of the current class. :)
May 28 2016
On Sunday, 29 May 2016 at 00:48:20 UTC, dan wrote:Especially in a declaration like static typeof(this) make_instance( ) but also in the 'new typeof(this)'. In both cases, 'this' doesn't even exist.https://dlang.org/spec/declaration.html#Typeof it's another 'this' that has not the same semantic as the reference holder. Just like 'const' can have 3 meanings, 'this' also: - this.member: typical usage, it hold the instance reference - void foo(this T)(): template this parameter, T is typeof(this) where the template is used. - typeof(this): you can use it in static func, this 'this' is not the 'this' instance.
May 28 2016
On Sunday, 29 May 2016 at 02:44:33 UTC, jhps wrote:On Sunday, 29 May 2016 at 00:48:20 UTC, dan wrote:OK, thanks JHPS for the detailed explanation of this construction Mithun pointed out, and also for the link. It makes a lot of sense the way you put it.Especially in a declaration like static typeof(this) make_instance( ) but also in the 'new typeof(this)'. In both cases, 'this' doesn't even exist.https://dlang.org/spec/declaration.html#Typeof it's another 'this' that has not the same semantic as the reference holder. Just like 'const' can have 3 meanings, 'this' also: - this.member: typical usage, it hold the instance reference - void foo(this T)(): template this parameter, T is typeof(this) where the template is used. - typeof(this): you can use it in static func, this 'this' is not the 'this' instance.
May 28 2016