Jump to page: 1 2
Thread overview
structure inheritance and polymorphism
Jan 04, 2021
zjh
Jan 04, 2021
Paul Backus
Jan 04, 2021
zjh
Jan 04, 2021
9il
Jan 04, 2021
zjh
Jan 06, 2021
ryuukk_
Jan 07, 2021
zjh
Jan 06, 2021
IGotD-
Jan 06, 2021
ryuukk_
Jan 07, 2021
zjh
Jan 07, 2021
zjh
Jan 07, 2021
sighoya
Jan 07, 2021
zjh
January 04, 2021
Can d be added with support for structure inheritance and polymorphism?
I like D, but I don't like GC, so I can't use classes. I can only use betterC. But I want d to have c++ structure inheritance and polymorphism. In this way, I think the competition with c++, the odds are better.
January 04, 2021
On Monday, 4 January 2021 at 01:06:20 UTC, zjh wrote:
> Can d be added with support for structure inheritance and polymorphism?
> I like D, but I don't like GC, so I can't use classes. I can only use betterC. But I want d to have c++ structure inheritance and polymorphism. In this way, I think the competition with c++, the odds are better.

You can use classes without the GC. You just have to allocate and free them manually, either with malloc/free or something like std.experimental.allocator. You may also find a smart-pointer library like automem useful for this:

https://code.dlang.org/packages/automem

If you want to avoid the D runtime completely, you cannot use "normal" D classes, but extern(C++) classes will still work.

If you absolutely must use structs, you can use "alias this" to achieve something very similar to inheritance:

https://dlang.org/spec/class.html#alias-this
January 04, 2021
On Monday, 4 January 2021 at 01:28:33 UTC, Paul Backus wrote:

Thank you for your hint.I'll try it.
January 04, 2021
On Monday, 4 January 2021 at 01:06:20 UTC, zjh wrote:
> Can d be added with support for structure inheritance and polymorphism?
> I like D, but I don't like GC, so I can't use classes. I can only use betterC. But I want d to have c++ structure inheritance and polymorphism. In this way, I think the competition with c++, the odds are better.

mir.rc.ptr [1] of mir-algorithm package provides thread-safe smart pointers that work with structs and classes.

It allows
 * converting a smart pointer to a base class smart pointer.
 * converting a smart pointer to "alias this" struct field.
 * converting a smart pointer to a smart pointer to any struct field without additional memory allocation (for structs).


mir.algebraic of mir-core provides [2] algebraic types that generate struct accessors if all allowed types have these public members. Also, these methods propagation is transparent for "alias this" members.

[1] http://mir-algorithm.libmir.org/mir_rc_ptr.html
[2] http://mir-core.libmir.org/mir_algebraic.html#.Algebraic (the first big example)

Kind regards,
Ilya



January 04, 2021
On Monday, 4 January 2021 at 13:24:51 UTC, 9il wrote:

Thanks for your reply. Mir is very good, I have heard the name of mir long ago.
I am a frequent forum novice.
Many people in the forum have good ideas. And the information is good. But many of them are underused.
I suggest that the D community should make full use of the good information in the forum.
There should be a summary of some of the discussions. Then put a separate page.
In this way, when it comes to related topics later, there is no need to repeat them
In my opinion, D community should be organized. Only the organized community can be powerful. Now, D, I feel that the community is too loose. It's like a random walker. There's no clear direction.

January 06, 2021
On Monday, 4 January 2021 at 15:04:14 UTC, zjh wrote:
> On Monday, 4 January 2021 at 13:24:51 UTC, 9il wrote:
>
> Thanks for your reply. Mir is very good, I have heard the name of mir long ago.
> I am a frequent forum novice.
> Many people in the forum have good ideas. And the information is good. But many of them are underused.
> I suggest that the D community should make full use of the good information in the forum.
> There should be a summary of some of the discussions. Then put a separate page.
> In this way, when it comes to related topics later, there is no need to repeat them
> In my opinion, D community should be organized. Only the organized community can be powerful. Now, D, I feel that the community is too loose. It's like a random walker. There's no clear direction.

D is very powerfull

Check: https://code.dlang.org/packages/sumtype

Also this: https://github.com/atilaneves/tardy

And this blog if you want to do something more custom: https://theartofmachinery.com/2018/08/13/inheritance_and_polymorphism_2.html
January 06, 2021
On Monday, 4 January 2021 at 01:06:20 UTC, zjh wrote:
> Can d be added with support for structure inheritance and polymorphism?
> I like D, but I don't like GC, so I can't use classes. I can only use betterC. But I want d to have c++ structure inheritance and polymorphism. In this way, I think the competition with c++, the odds are better.

I think that mixin templates is something you should look at. It is not inheritance but composition. Unfortunately mixin templates are badly documented explaining how they can be used as an alternative to inheritance or alias this.

Basically one way to emulate polymorphism is to use opCast, returning an inner member variable. The implicit cast like "operator Type()" in C++ is not available in D to begin with so the casting must be stated anyway.

Keep in mind that you can use classes without GC. For exampled using the scoped macro or manually allocated.
January 06, 2021
On Wednesday, 6 January 2021 at 17:58:24 UTC, IGotD- wrote:

> Keep in mind that you can use classes without GC. For exampled using the scoped macro or manually allocated.

but there is a big problem with that, is you need shop GC, and you need be careful because you can't enforce a @nogc globally, so you either tag every functions with @nogc wich is a pain and add noise everywhere in your files, sure you can have @nogc:  bellow the module but it doesn't work for functions inside classes/structs


-betterC at least is enforced globally, so you don't think about hidden memory allocation that's gonna stack as your program lives


i believe interface with struct is a good idea because it avoids having to write lot of boilerplate to emulate vtables

or better, the concept of "signature", since most of the time, we use interface to make sure the type has that specific method..

interface on struct would lower barrier of entry by a LOT
January 07, 2021
Thanks very much for above reply. You(s) are all right.

I'm still of that opinion. D to develop. Leakages must be filled.

And the structure of D is too weak, just a structure like C. Must be strengthened to c++ version.

And then there's the standard library. Every time I think about GC, I'm afraid to use the standard library.

January 07, 2021
On Wednesday, 6 January 2021 at 17:58:24 UTC, IGotD- wrote:
> On Monday, 4 January 2021 at 01:06:20 UTC, zjh wrote:

mixin vs inherit,inherit is more cool. has one and be a one, the difference is clear.




« First   ‹ Prev
1 2