www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - structure inheritance and polymorphism

reply zjh <fqbqrr 163.com> writes:
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.
Jan 03 2021
next sibling parent reply Paul Backus <snarwin gmail.com> writes:
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
Jan 03 2021
parent zjh <fqbqrr 163.com> writes:
On Monday, 4 January 2021 at 01:28:33 UTC, Paul Backus wrote:

Thank you for your hint.I'll try it.
Jan 03 2021
prev sibling next sibling parent reply 9il <ilyayaroshenko gmail.com> writes:
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 first big example) Kind regards, Ilya
Jan 04 2021
parent reply zjh <fqbqrr 163.com> writes:
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.
Jan 04 2021
parent reply ryuukk_ <ryuukk_ gmail.com> writes:
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
Jan 06 2021
parent zjh <fqbqrr 163.com> writes:
On Wednesday, 6 January 2021 at 16:54:29 UTC, ryuukk_ wrote:

`d` is powerful. But d's `struct` is not . D's `gc` push users to 
other languages, dragging the legs.d's betterC has no associative 
array.
for users,they want to work like c++,because many of them has c++ 
background.
Jan 06 2021
prev sibling next sibling parent reply IGotD- <nise nise.com> writes:
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.
Jan 06 2021
next sibling parent ryuukk_ <ryuukk_ gmail.com> writes:
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
Jan 06 2021
prev sibling parent zjh <fqbqrr 163.com> writes:
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.
Jan 06 2021
prev sibling next sibling parent zjh <fqbqrr 163.com> writes:
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.
Jan 06 2021
prev sibling parent reply sighoya <sighoya gmail.com> writes:
On Monday, 4 January 2021 at 01:06:20 UTC, zjh wrote:
But I want d to have c++ structure
 inheritance and polymorphism. In this way, I think the 
 competition with c++, the odds are better.
Structs as polymorphic structures are bad because structs shouldn't be polymorphic, otherwise they have to store a vtable somewhere. If you don't like classes, what about interfaces? The only problem is that they don't support fields which I find myself a bit pitty. You can however simulate it with getters and setters, at least to some kind. With https://dlang.org/phobos/std_experimental_typecons.html you can box a struct to an interface or a class. I don't know how stable it is yet, but I find it pretty interesting.
Jan 07 2021
parent zjh <fqbqrr 163.com> writes:
On Thursday, 7 January 2021 at 11:24:14 UTC, sighoya wrote:

I don't like quirky skills. I like to use it out of the box. Just 
like C++, C++ can be used directly.what you say is right, since 
the structure should not be polymorphic. Then, D can make 
something similar to structure / class/interface, because there 
are many kinds classes. It doesn't need to have a root class like 
Object. It also has a hierarchical.may be have to use 
`extern(c++) class`?
However, the current structure still does not support 
inheritance. Even if it does not need polymorphism, inheritance 
is good, because it can simplify a lot of code. I don't agree 
with the exclusion of D structure on inheritance. Inheritance is 
a good tool.
Jan 07 2021