Thread overview
parent class get the subclass object
Jan 16, 2017
Brian
Jan 16, 2017
rikki cattermole
Jan 16, 2017
Brian
Jan 16, 2017
Nemanja Boric
Jan 17, 2017
Brian
Jan 16, 2017
Mike Parker
Jan 17, 2017
Dsby
Jan 17, 2017
Brian
January 16, 2017
Dlang should support such coding. What should I do?

import std.stdio : writeln;

abstract class Base(T)
{
    this()
    {
        _this = this;
    }

    void hello()
    {
        _this.world();
    }

    private
    {
        T _this;
    }
}


class Sub : Base!Sub
{
    void world()
    {
        writeln("Hello world");
    }
}

void main()
{
    Sub sub = new Sub;
    sub.hello();
}

January 17, 2017
On 17/01/2017 1:15 AM, Brian wrote:
> Dlang should support such coding. What should I do?
>
> import std.stdio : writeln;
>
> abstract class Base(T)
> {
>     this()
>     {
>         _this = this;
>     }
>
>     void hello()
>     {
>         _this.world();
>     }
>
>     private
>     {
>         T _this;
>     }
> }
>
>
> class Sub : Base!Sub
> {
>     void world()
>     {
>         writeln("Hello world");
>     }
> }
>
> void main()
> {
>     Sub sub = new Sub;
>     sub.hello();
> }
>

FYI, this would have been more appropriate in D.learn instead of the main forum.

import std.stdio;

abstract class Base {
	void hello() {
		world();
	}

	void world();
}

class Sub : Base {
	override void world() {
		writeln("Hello World");
	}
}

void main() {
	Sub sub = new Sub;
	sub.hello();
}
January 16, 2017
On Monday, 16 January 2017 at 12:20:47 UTC, rikki cattermole wrote:
> On 17/01/2017 1:15 AM, Brian wrote:
>> Dlang should support such coding. What should I do?
>>
>> import std.stdio : writeln;
>>
>> abstract class Base(T)
>> {
>>     this()
>>     {
>>         _this = this;
>>     }
>>
>>     void hello()
>>     {
>>         _this.world();
>>     }
>>
>>     private
>>     {
>>         T _this;
>>     }
>> }
>>
>>
>> class Sub : Base!Sub
>> {
>>     void world()
>>     {
>>         writeln("Hello world");
>>     }
>> }
>>
>> void main()
>> {
>>     Sub sub = new Sub;
>>     sub.hello();
>> }
>>
>
> FYI, this would have been more appropriate in D.learn instead of the main forum.
>
> import std.stdio;
>
> abstract class Base {
> 	void hello() {
> 		world();
> 	}
>
> 	void world();
> }
>
> class Sub : Base {
> 	override void world() {
> 		writeln("Hello World");
> 	}
> }
>
> void main() {
> 	Sub sub = new Sub;
> 	sub.hello();
> }

No, you don't understand I want to express meaning.

other programing language is allow this.

Your code more like the old C++.

If a high-level programing language or need haevy like C++ impl code, It's very regret.

Can like rust / swift / php / java / C# ?
January 16, 2017
On Monday, 16 January 2017 at 16:31:41 UTC, Brian wrote:
> On Monday, 16 January 2017 at 12:20:47 UTC, rikki cattermole wrote:
>> On 17/01/2017 1:15 AM, Brian wrote:
>>> Dlang should support such coding. What should I do?
>>>
>>> import std.stdio : writeln;
>>>
>>> abstract class Base(T)
>>> {
>>>     this()
>>>     {
>>>         _this = this;
>>>     }
>>>
>>>     void hello()
>>>     {
>>>         _this.world();
>>>     }
>>>
>>>     private
>>>     {
>>>         T _this;
>>>     }
>>> }
>>>
>>>
>>> class Sub : Base!Sub
>>> {
>>>     void world()
>>>     {
>>>         writeln("Hello world");
>>>     }
>>> }
>>>
>>> void main()
>>> {
>>>     Sub sub = new Sub;
>>>     sub.hello();
>>> }
>>>
>>
>> FYI, this would have been more appropriate in D.learn instead of the main forum.
>>
>> import std.stdio;
>>
>> abstract class Base {
>> 	void hello() {
>> 		world();
>> 	}
>>
>> 	void world();
>> }
>>
>> class Sub : Base {
>> 	override void world() {
>> 		writeln("Hello World");
>> 	}
>> }
>>
>> void main() {
>> 	Sub sub = new Sub;
>> 	sub.hello();
>> }
>
> No, you don't understand I want to express meaning.
>
> other programing language is allow this.
>
> Your code more like the old C++.
>
> If a high-level programing language or need haevy like C++ impl code, It's very regret.
>
> Can like rust / swift / php / java / C# ?

You're missing a cast, since `this` is a reference to a superclass.

import std.stdio : writeln;

abstract class Base(T)
{
    this()
    {
        _this = cast(T)(this);
        assert(_this);
    }

    void hello()
    {
        _this.world();
    }

    private
    {
        T _this;
    }
}


class Sub : Base!Sub
{
    void world()
    {
        writeln("Hello world");
    }
}

void main()
{
    Sub sub = new Sub;
    sub.hello();
}

January 16, 2017
On Monday, 16 January 2017 at 16:31:41 UTC, Brian wrote:

> No, you don't understand I want to express meaning.
>
> other programing language is allow this.
>
> Your code more like the old C++.
>
> If a high-level programing language or need haevy like C++ impl code, It's very regret.
>
> Can like rust / swift / php / java / C# ?

Perhaps template this parameters [1]:

```
class Base {
    void hello(this C)() {
        (cast(C)this).world();
    }
}

class Sub:Base {
    void world() { writeln("Hello world"); }
}

void main() {
    auto sub = new Sub;
    sub.hello();
}
```

[1] http://dlang.org/spec/template.html#TemplateThisParameter

January 17, 2017
On Monday, 16 January 2017 at 19:31:50 UTC, Mike Parker wrote:
> On Monday, 16 January 2017 at 16:31:41 UTC, Brian wrote:
>
>> No, you don't understand I want to express meaning.
>>
>> other programing language is allow this.
>>
>> Your code more like the old C++.
>>
>> If a high-level programing language or need haevy like C++ impl code, It's very regret.
>>
>> Can like rust / swift / php / java / C# ?
>
> Perhaps template this parameters [1]:
>
> ```
> class Base {
>     void hello(this C)() {
>         (cast(C)this).world();
>     }
> }
>
> class Sub:Base {
>     void world() { writeln("Hello world"); }
> }
>
> void main() {
>     auto sub = new Sub;
>     sub.hello();
> }
> ```
>
> [1] http://dlang.org/spec/template.html#TemplateThisParameter

That  Great!
January 17, 2017
On Monday, 16 January 2017 at 19:31:50 UTC, Mike Parker wrote:
> On Monday, 16 January 2017 at 16:31:41 UTC, Brian wrote:
>
>> No, you don't understand I want to express meaning.
>>
>> other programing language is allow this.
>>
>> Your code more like the old C++.
>>
>> If a high-level programing language or need haevy like C++ impl code, It's very regret.
>>
>> Can like rust / swift / php / java / C# ?
>
> Perhaps template this parameters [1]:
>
> ```
> class Base {
>     void hello(this C)() {
>         (cast(C)this).world();
>     }
> }
>
> class Sub:Base {
>     void world() { writeln("Hello world"); }
> }
>
> void main() {
>     auto sub = new Sub;
>     sub.hello();
> }
> ```
>
> [1] http://dlang.org/spec/template.html#TemplateThisParameter

Thanks, I think this is the best way!
January 17, 2017
On Monday, 16 January 2017 at 16:47:09 UTC, Nemanja Boric wrote:
> On Monday, 16 January 2017 at 16:31:41 UTC, Brian wrote:
>> [...]
>
> You're missing a cast, since `this` is a reference to a superclass.
>
> import std.stdio : writeln;
>
> abstract class Base(T)
> {
>     this()
>     {
>         _this = cast(T)(this);
>         assert(_this);
>     }
>
>     void hello()
>     {
>         _this.world();
>     }
>
>     private
>     {
>         T _this;
>     }
> }
>
>
> class Sub : Base!Sub
> {
>     void world()
>     {
>         writeln("Hello world");
>     }
> }
>
> void main()
> {
>     Sub sub = new Sub;
>     sub.hello();
> }

Yes, it's possible, thank you!