digitalmars.D.learn - Operator Overloading with class template
- Eyyub (6/6) Apr 08 2012 Hello,
- Francois Chabot (16/22) Apr 08 2012 As far as I know, there is nothing special about themplate
- Francois Chabot (3/27) Apr 08 2012 Nevermind, read your message too quickly... I thought it was
- Eyyub (2/34) Apr 08 2012 Np :D, you don't know how can I do for the example 2 ?
- Andrej Mitrovic (4/5) Apr 08 2012 Well for one thing, there are no global operators in D. Others might
- Eyyub (4/10) Apr 08 2012 Like this : http://paste.pocoo.org/show/VupvqFTLqZxvZajw71MA/ ? I
- James Miller (29/38) Apr 09 2012 What you want is something like this:
- Eyyub (4/51) Apr 09 2012 Ho, yeah it works !!! :D :D
Hello, How can I rewrite the exemple 2 (http://pastebin.com/q50903Zh) in D lang. ? This source code doesn't work...why ? http://paste.pocoo.org/show/wy1kDIpqTi2ApRuOxRRb/ Thx. :)
Apr 08 2012
On Sunday, 8 April 2012 at 23:14:33 UTC, Eyyub wrote:Hello, How can I rewrite the exemple 2 (http://pastebin.com/q50903Zh) in D lang. ? This source code doesn't work...why ? http://paste.pocoo.org/show/wy1kDIpqTi2ApRuOxRRb/ Thx. :)As far as I know, there is nothing special about themplate classes with regards to operator overloading, so you can follow the examples in there: http://dlang.org/operatoroverloading.html which in your example would yield: Matrix opOpAssign(string op)( Matrix rhs ) { for( size_t i =0 ; i < M ; ++i ) { for( size_t j =0 ; j < N ; ++j ) { mixin("array_[i*N+j]" ~ op ~ " rhs.array_[i*N+j] ;") ; } } } Bonus! all ...= operators implemented at once!
Apr 08 2012
On Sunday, 8 April 2012 at 23:41:51 UTC, Francois Chabot wrote:On Sunday, 8 April 2012 at 23:14:33 UTC, Eyyub wrote:Nevermind, read your message too quickly... I thought it was about the matrix class...Hello, How can I rewrite the exemple 2 (http://pastebin.com/q50903Zh) in D lang. ? This source code doesn't work...why ? http://paste.pocoo.org/show/wy1kDIpqTi2ApRuOxRRb/ Thx. :)As far as I know, there is nothing special about themplate classes with regards to operator overloading, so you can follow the examples in there: http://dlang.org/operatoroverloading.html which in your example would yield: Matrix opOpAssign(string op)( Matrix rhs ) { for( size_t i =0 ; i < M ; ++i ) { for( size_t j =0 ; j < N ; ++j ) { mixin("array_[i*N+j]" ~ op ~ " rhs.array_[i*N+j] ;") ; } } } Bonus! all ...= operators implemented at once!
Apr 08 2012
On Sunday, 8 April 2012 at 23:44:12 UTC, Francois Chabot wrote:On Sunday, 8 April 2012 at 23:41:51 UTC, Francois Chabot wrote:Np :D, you don't know how can I do for the example 2 ?On Sunday, 8 April 2012 at 23:14:33 UTC, Eyyub wrote:Nevermind, read your message too quickly... I thought it was about the matrix class...Hello, How can I rewrite the exemple 2 (http://pastebin.com/q50903Zh) in D lang. ? This source code doesn't work...why ? http://paste.pocoo.org/show/wy1kDIpqTi2ApRuOxRRb/ Thx. :)As far as I know, there is nothing special about themplate classes with regards to operator overloading, so you can follow the examples in there: http://dlang.org/operatoroverloading.html which in your example would yield: Matrix opOpAssign(string op)( Matrix rhs ) { for( size_t i =0 ; i < M ; ++i ) { for( size_t j =0 ; j < N ; ++j ) { mixin("array_[i*N+j]" ~ op ~ " rhs.array_[i*N+j] ;") ; } } } Bonus! all ...= operators implemented at once!
Apr 08 2012
On 4/9/12, Eyyub <eyyub.pangearaion gmail.com> wrote:Np :D, you don't know how can I do for the example 2 ?Well for one thing, there are no global operators in D. Others might help out with writing a proper opBinary that's defined in Value itself.
Apr 08 2012
On Monday, 9 April 2012 at 00:04:50 UTC, Andrej Mitrovic wrote:On 4/9/12, Eyyub <eyyub.pangearaion gmail.com> wrote:Like this : http://paste.pocoo.org/show/VupvqFTLqZxvZajw71MA/ ? I have the same errors :s ThanksNp :D, you don't know how can I do for the example 2 ?Well for one thing, there are no global operators in D. Others might help out with writing a proper opBinary that's defined in Value itself.
Apr 08 2012
* Eyyub <eyyub.pangearaion gmail.com> [2012-04-09 01:14:32 +0200]:Hello, How can I rewrite the exemple 2 (http://pastebin.com/q50903Zh) in D lang. ? This source code doesn't work...why ? http://paste.pocoo.org/show/wy1kDIpqTi2ApRuOxRRb/ Thx. :)What you want is something like this: class Value(int M, int K, int S) { private { float _value; } public { this(float val) { _value = val; } auto opBinary(string op : "/", int MO, int KO, int SO)(Value(MO, KO, SO) other) { return new Value!(M-MO, K-KO, S-SO)(_value/other.value); } } } A Few notes: 1. This isn't tested, might not work, but thats where you should aim... 2. Operator overloading is implemented as a lowering (as far as I'm aware) the compiler emits a template instantiation of opBinary("/") for the type. That means that you can add on more template arguments. 3. Using `auto` means that the compiler works out the type, so you don't have to add extra template arguments to calculate the correct type. Hope that helps. -- James Miller
Apr 09 2012
On Monday, 9 April 2012 at 09:09:05 UTC, James Miller wrote:* Eyyub <eyyub.pangearaion gmail.com> [2012-04-09 01:14:32 +0200]:Ho, yeah it works !!! :D :D http://paste.pocoo.org/show/ye7VXQHfLxsVM38bKyS6/ Thanks a lot !Hello, How can I rewrite the exemple 2 (http://pastebin.com/q50903Zh) in D lang. ? This source code doesn't work...why ? http://paste.pocoo.org/show/wy1kDIpqTi2ApRuOxRRb/ Thx. :)What you want is something like this: class Value(int M, int K, int S) { private { float _value; } public { this(float val) { _value = val; } auto opBinary(string op : "/", int MO, int KO, int SO)(Value(MO, KO, SO) other) { return new Value!(M-MO, K-KO, S-SO)(_value/other.value); } } } A Few notes: 1. This isn't tested, might not work, but thats where you should aim... 2. Operator overloading is implemented as a lowering (as far as I'm aware) the compiler emits a template instantiation of opBinary("/") for the type. That means that you can add on more template arguments. 3. Using `auto` means that the compiler works out the type, so you don't have to add extra template arguments to calculate the correct type. Hope that helps. -- James Miller
Apr 09 2012