digitalmars.D - assign value to *this like in c++?
- lyan (10/10) Jun 20 2019 Hi!
- lyan (3/13) Jun 20 2019 The code is C++ btw. and I had a typo below, I meant /= and *=
- Basile B. (10/27) Jun 20 2019 Use `void opOpAssign(string op, T)(auto ref T t);` model to
- Nicholas Wilson (9/26) Jun 20 2019 If you're doing the porting to learn then what the others have
- XavierAP (13/23) Jun 20 2019 In C++ "this" is a pointer, but in D it's a reference -- so this
- lyan (9/9) Jun 20 2019 Thanks, those are very useful links.
Hi! I just started with D, read already a bit through the documentation but I'm currently stuck here at porting my vector3 class to D: inline void normalize() { *this /= length(); } How can I achieve that in D? Also: is there a way to overload /=, */ operators? Thanks for the help!
Jun 20 2019
On Thursday, 20 June 2019 at 11:28:53 UTC, lyan wrote:Hi! I just started with D, read already a bit through the documentation but I'm currently stuck here at porting my vector3 class to D: inline void normalize() { *this /= length(); } How can I achieve that in D? Also: is there a way to overload /=, */ operators? Thanks for the help!The code is C++ btw. and I had a typo below, I meant /= and *= operators.
Jun 20 2019
On Thursday, 20 June 2019 at 11:31:19 UTC, lyan wrote:On Thursday, 20 June 2019 at 11:28:53 UTC, lyan wrote:Use `void opOpAssign(string op, T)(auto ref T t);` model to overload "DivAssisng" and "MulAssign". The compiler will instantiate the function template with the right `op` and `T` info when used in the code. These template parameters can be tested in a `static if` expression. See http://ddili.org/ders/d.en/operator_overloading.html for learning D operator overloads. Note that these kind of questions should go in .learn : https://forum.dlang.org/group/learnHi! I just started with D, read already a bit through the documentation but I'm currently stuck here at porting my vector3 class to D: inline void normalize() { *this /= length(); } How can I achieve that in D? Also: is there a way to overload /=, */ operators? Thanks for the help!The code is C++ btw. and I had a typo below, I meant /= and *= operators.
Jun 20 2019
On Thursday, 20 June 2019 at 11:31:19 UTC, lyan wrote:On Thursday, 20 June 2019 at 11:28:53 UTC, lyan wrote:If you're doing the porting to learn then what the others have said will get you there. If you wan't to just get started with some 3d math there is https://github.com/d-gamedev-team/gfm (under math/gfm/math). Also useful to see what tricks they do to implement the features. It you're new I can't recommend http://ddili.org/ders/d.en/index.html enough. In future please post such questions to the learn forum.Hi! I just started with D, read already a bit through the documentation but I'm currently stuck here at porting my vector3 class to D: inline void normalize() { *this /= length(); } How can I achieve that in D? Also: is there a way to overload /=, */ operators? Thanks for the help!The code is C++ btw. and I had a typo below, I meant /= and *= operators.
Jun 20 2019
On Thursday, 20 June 2019 at 11:28:53 UTC, lyan wrote:Hi! I just started with D, read already a bit through the documentation but I'm currently stuck here at porting my vector3 class to D: inline void normalize() { *this /= length(); } How can I achieve that in D? Also: is there a way to overload /=, */ operators? Thanks for the help!In C++ "this" is a pointer, but in D it's a reference -- so this should work: this /= length(); BTW the empty () in function calls is optional in D; and see also "UFCS", which Bjarne S. wanted to get into C++ but ISO rejected it. Yes it's possible to overload operators: https://dlang.org/spec/operatoroverloading.html#op-assign Note that these reserved functions to overload operators are templates. Therefore it's possible to generate code for several operators (e.g. + and - or more) with a single template and mixins; as shown in the examples in the link above.
Jun 20 2019
Thanks, those are very useful links. nogc Vector opUnary(string op)() pure const nothrow if (op == "+" || op == "-" || op == "~" || op == "!") { Vector res = void; mixin(generateLoopCode!("res.v[ ] = " ~ op ~ " v[ ];", N)()); return res; } wow...
Jun 20 2019