www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Two way struct wrapper

reply drug <drug2004 bk.ru> writes:
Using `alias this` it's easy to make wrapper for structure that calls 
wrapped structure methods like its own. This is one way - from wrapper 
to wrapped transformation. Is it possible to create the opposite way 
from wrapped to wrapper?

https://run.dlang.io/is/Avyu3I

All calls to Bar is redirected to Foo, but output of Foo is not 
redirected to Bar.
Oct 11 2017
next sibling parent Igor <stojkovic.igor gmail.com> writes:
On Wednesday, 11 October 2017 at 12:35:51 UTC, drug wrote:
 Using `alias this` it's easy to make wrapper for structure that 
 calls wrapped structure methods like its own. This is one way - 
 from wrapper to wrapped transformation. Is it possible to 
 create the opposite way from wrapped to wrapper?

 https://run.dlang.io/is/Avyu3I

 All calls to Bar is redirected to Foo, but output of Foo is not 
 redirected to Bar.
I don't see how compiler can just deduce that... In either case you can just wrap the expression with another Bar(): auto v5 = Bar(Bar(Foo(2)) + Bar(Foo(3)));
Oct 13 2017
prev sibling parent reply Alex <sascha.orlov gmail.com> writes:
On Wednesday, 11 October 2017 at 12:35:51 UTC, drug wrote:
 Using `alias this` it's easy to make wrapper for structure that 
 calls wrapped structure methods like its own. This is one way - 
 from wrapper to wrapped transformation. Is it possible to 
 create the opposite way from wrapped to wrapper?

 https://run.dlang.io/is/Avyu3I

 All calls to Bar is redirected to Foo, but output of Foo is not 
 redirected to Bar.
How about a template for the opBinary operator? https://run.dlang.io/is/7gi5Wl The code relies on the fact, that only one alias this is allowed. https://dlang.org/spec/class.html#AliasThis If more are possible at some moment, I suppose it will be possible to static foreach over them to choose the right member, based on its type.
Oct 14 2017
parent reply Alex <sascha.orlov gmail.com> writes:
On Saturday, 14 October 2017 at 11:25:20 UTC, Alex wrote:
of course, with the proper operator in line 32

return T(mixin("this" ~ op ~ "f"));

https://run.dlang.io/is/jGKVYN
Oct 14 2017
parent reply Alex <sascha.orlov gmail.com> writes:
On Saturday, 14 October 2017 at 12:39:17 UTC, Alex wrote:

ok, the last version for now. Without assumptions on alias 
number. Sorry for noise.

https://run.dlang.io/is/OSJYtY
Oct 14 2017
parent drug <drug2004 bk.ru> writes:
14.10.2017 16:23, Alex пишет:
 On Saturday, 14 October 2017 at 12:39:17 UTC, Alex wrote:
 
 ok, the last version for now. Without assumptions on alias number. Sorry 
 for noise.
 
 https://run.dlang.io/is/OSJYtY
That's cool, but unfortunately demands access to source code of Foo, it's not my case( I improved your example a little bit - https://run.dlang.io/is/yeKofN, it's really great. Need polish of course, but this is a way to get more inheritance with structures.
Oct 14 2017