www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - assign value to *this like in c++?

reply lyan <nothanks 0.0> writes:
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
next sibling parent reply lyan <nothanks 0.0> writes:
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
next sibling parent Basile B. <b2.temp gmx.com> writes:
On Thursday, 20 June 2019 at 11:31:19 UTC, lyan wrote:
 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.
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/learn
Jun 20
prev sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Thursday, 20 June 2019 at 11:31:19 UTC, lyan wrote:
 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.
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.
Jun 20
prev sibling next sibling parent XavierAP <n3minis-git yahoo.es> writes:
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
prev sibling parent lyan <nothanks 0.0> writes:
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