Re: Operator Overloading with class template

2012-04-09 Thread James Miller
* Eyyub eyyub.pangeara...@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


Re: Operator Overloading with class template

2012-04-09 Thread Eyyub

On Monday, 9 April 2012 at 09:09:05 UTC, James Miller wrote:
* Eyyub eyyub.pangeara...@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


Ho, yeah it works !!! :D :D

http://paste.pocoo.org/show/ye7VXQHfLxsVM38bKyS6/

Thanks a lot !


Re: Operator Overloading with class template

2012-04-08 Thread Francois Chabot

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!


Re: Operator Overloading with class template

2012-04-08 Thread Francois Chabot

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:

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!


Nevermind, read your message too quickly... I thought it was 
about the matrix class...




Re: Operator Overloading with class template

2012-04-08 Thread Eyyub

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:

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!


Nevermind, read your message too quickly... I thought it was 
about the matrix class...


Np :D, you don't know how can I do for the example 2 ?


Re: Operator Overloading with class template

2012-04-08 Thread Andrej Mitrovic
On 4/9/12, Eyyub eyyub.pangeara...@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.


Re: Operator Overloading with class template

2012-04-08 Thread Eyyub

On Monday, 9 April 2012 at 00:04:50 UTC, Andrej Mitrovic wrote:

On 4/9/12, Eyyub eyyub.pangeara...@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.


Like this : http://paste.pocoo.org/show/VupvqFTLqZxvZajw71MA/ ? I 
have the same errors :s


Thanks