www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Overloading + on points

reply Eduardo Cavazos <wayo.cavazos gmail.com> writes:
Hello,

Here's a short program which creates a type for points and overloads '+' 
to do element wise addition as well as addition to floats, however it 
produces an error:

----------------------------------------------------------------------
import std.stdio ;

struct Pt
{
   float x , y ;

   Pt opBinary ( string op ) ( Pt a ) if ( op == "+" )
     { return Pt ( x + a.x , y + a.y ) ; }

   Pt opBinary ( string op ) ( float a ) if ( op == "+" )
     { return Pt ( x + a , y + a ) ; }
}

void main () { Pt ( 1.0 , 2.0 ) + Pt ( 3.0 , 4.0 ) ; }
----------------------------------------------------------------------

The error:

pt_overload_test_a.d(15): Error: template instance opBinary!("+") 
matches more than one template declaration, 
pt_overload_test_a.d(8):opBinary(string op) if (op == "+") and 
pt_overload_test_a.d(11):opBinary(string op) if (op == "+")

So, how should I go about this? :-)

Ed
Aug 31 2010
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Eduardo Cavazos <wayo.cavazos gmail.com> wrote:

 Hello,

 Here's a short program which creates a type for points and overloads '+'  
 to do element wise addition as well as addition to floats, however it  
 produces an error:

 ----------------------------------------------------------------------
 import std.stdio ;

 struct Pt
 {
    float x , y ;

    Pt opBinary ( string op ) ( Pt a ) if ( op == "+" )
      { return Pt ( x + a.x , y + a.y ) ; }

    Pt opBinary ( string op ) ( float a ) if ( op == "+" )
      { return Pt ( x + a , y + a ) ; }
 }

 void main () { Pt ( 1.0 , 2.0 ) + Pt ( 3.0 , 4.0 ) ; }
 ----------------------------------------------------------------------

 The error:

 pt_overload_test_a.d(15): Error: template instance opBinary!("+")  
 matches more than one template declaration,  
 pt_overload_test_a.d(8):opBinary(string op) if (op == "+") and  
 pt_overload_test_a.d(11):opBinary(string op) if (op == "+")

 So, how should I go about this? :-)
That is indeed a perplexing error. It is caused by dmd not knowing how to overload template functions based on both normal and template parameters. As for the solution: Pt opBinary( string op : "+", T : Pt )( T a ) {...} Pt opBinary( string op : "+", T : float )( T a ) {...} should work. More explicitly: Pt opBinary( string op, T )( T a ) if ( ( op == "+" ) && is( T == Pt ) ) {...} Pt opBinary( string op, T )( T a ) if ( ( op == "+" ) && is( T == float ) ) {...} -- Simen
Aug 31 2010
parent reply Joel Christensen <joelcnz gmail.com> writes:

Stanislav Blinov <blinov loniir.ru> writes: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Hello, I'm receiving strange results with reading stdin on Windows 7. Consider this code: module test; import std.stdio; void main(string[] args) { foreach (int i, string line; lines(stdin)) { write(line); } } On Linux, if I do 'cat test.d | ./test' I get test.d contents on stdout. But on Windows 7, ('type test.d | ./test.exe') the output is this: std.stdio.StdioException: Bad file descriptor module test; import std.stdio; void main(string[] args) { foreach (int i, string line; lines(stdin)) { writef(line); } } So I too get type.d contents on stdout, but preceeded by StdioException string. This happens with dmd 2.047 and 2.048. Is this my error, dmd's, or Windows's piping? -- Aug 17 <<
Jesse Phillips <jessekphillips+D gmail.com>
In my experience Windows hasn't gotten piping right. And it has been 
known to
have bugs, this might be related:
http://stackoverflow.com/questions/466801/python-piping-on-windows-why-does-this-not-work
 Aug 18
Joel Christensen <joelcnz gmail.com>: Just dug up this problem. I have trouble with Thunderbird and so couldn't put reply. I've tried that solution on my Windows 7, but got no effect at all. Sept 1
Aug 31 2010
parent reply Andrej Mitrovic <andrej.mitrovich test.com> writes:
I get the same thing on XP.

If you swap the two like so:
type.exe | type.d

Then it works but it will wait on exit.

This works nicely:

type.exe < type.d

Joel Christensen Wrote:

  >>
 Stanislav Blinov <blinov loniir.ru> writes:
 Content-Type: text/plain; charset=UTF-8; format=flowed
 Content-Transfer-Encoding: 7bit
 
    Hello,
 
 I'm receiving strange results with reading stdin on Windows 7. Consider
 this code:
 
 module test;
 
 import std.stdio;
 
 void main(string[] args)
 {
           foreach (int i, string line; lines(stdin))
           {
                   write(line);
           }
 }
 
 On Linux, if I do 'cat test.d | ./test' I get test.d contents on stdout.
 But on Windows 7, ('type test.d | ./test.exe') the output is this:
 
 std.stdio.StdioException: Bad file descriptor
 module test;
 
 import std.stdio;
 
 void main(string[] args)
 {
           foreach (int i, string line; lines(stdin))
           {
                   writef(line);
           }
 }
 
 So I too get type.d contents on stdout, but preceeded by StdioException
 string. This happens with dmd 2.047 and 2.048.
 
 Is this my error, dmd's, or Windows's piping?
 -- 
 Aug 17
 
 <<
 
  >Jesse Phillips <jessekphillips+D gmail.com>
  >In my experience Windows hasn't gotten piping right. And it has been 
  >known to
  >have bugs, this might be related:
  >http://stackoverflow.com/questions/466801/python-piping-on-windows-why-does-this-not-work
  > Aug 18
 
 Joel Christensen <joelcnz gmail.com>:
 Just dug up this problem. I have trouble with Thunderbird and so 
 couldn't put reply.
 
 I've tried that solution on my Windows 7, but got no effect at all.
 Sept 1
Aug 31 2010
parent reply Andrej Mitrovic <andrej.mitrovich test.com> writes:
Oooops, I meant:

std.stdio.StdioException: Bad file descriptor
type test.d | test.exe

works, but waits on exit:
test.exe | type test.d

works fine:
test.exe < test.d

Andrej Mitrovic Wrote:

 I get the same thing on XP.
 
 If you swap the two like so:
 type.exe | type.d
 
 Then it works but it will wait on exit.
 
 This works nicely:
 
 type.exe < type.d
 
 Joel Christensen Wrote:
 
  >>
 Stanislav Blinov <blinov loniir.ru> writes:
 Content-Type: text/plain; charset=UTF-8; format=flowed
 Content-Transfer-Encoding: 7bit
 
    Hello,
 
 I'm receiving strange results with reading stdin on Windows 7. Consider
 this code:
 
 module test;
 
 import std.stdio;
 
 void main(string[] args)
 {
           foreach (int i, string line; lines(stdin))
           {
                   write(line);
           }
 }
 
 On Linux, if I do 'cat test.d | ./test' I get test.d contents on stdout.
 But on Windows 7, ('type test.d | ./test.exe') the output is this:
 
 std.stdio.StdioException: Bad file descriptor
 module test;
 
 import std.stdio;
 
 void main(string[] args)
 {
           foreach (int i, string line; lines(stdin))
           {
                   writef(line);
           }
 }
 
 So I too get type.d contents on stdout, but preceeded by StdioException
 string. This happens with dmd 2.047 and 2.048.
 
 Is this my error, dmd's, or Windows's piping?
 -- 
 Aug 17
 
 <<
 
  >Jesse Phillips <jessekphillips+D gmail.com>
  >In my experience Windows hasn't gotten piping right. And it has been 
  >known to
  >have bugs, this might be related:
  >http://stackoverflow.com/questions/466801/python-piping-on-windows-why-does-this-not-work
  > Aug 18
 
 Joel Christensen <joelcnz gmail.com>:
 Just dug up this problem. I have trouble with Thunderbird and so 
 couldn't put reply.
 
 I've tried that solution on my Windows 7, but got no effect at all.
 Sept 1
Aug 31 2010
parent Joel Christensen <joelcnz gmail.com> writes:
On 01-Sep-10 12:54 PM, Andrej Mitrovic wrote:
 Oooops, I meant:

 std.stdio.StdioException: Bad file descriptor
 type test.d | test.exe

 works, but waits on exit:
 test.exe | type test.d

 works fine:
 test.exe<  test.d
I think I get the same as you. Have to put Ctrl+C (or some thing) to get out, for 'test.exe | type test.d' I recon I can use 'test.exe < test.d' sort of thing, any how. It'd complement 'test.exe > test.txt' (which I have knowen and do use some times).
Sep 02 2010