Oberon Community Platform Forum
September 25, 2017, 08:51:00 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Ambiguity with overloaded operator  (Read 2808 times)
hreba
Newbie
*
Posts: 14


« on: February 02, 2009, 07:46:42 PM »

The program below defines a type Vector and 3 types of multiplication: (real,Vector) -> Vector, (Vector,real) -> Vector and (Vector,Vector) -> real.
Code:
module Raytracing;

type Vector = array 3 of real;

operator '*' (a:real; v:Vector): Vector;
var res: Vector;
begin
  res[0]:= a*v[0];  res[1]:= a*v[1];  res[2]:= a*v[2];
  return res;
end '*';

operator '*' (v:Vector; a:real): Vector;
begin return a*v;
end '*';

operator '*' (v,w:Vector): real;
begin
  return v[0]*w[0] + v[1]*w[1] + v[2]*w[2];
end '*';

definition Surface;
  var pos, dir: Vector;
end Surface;

type Circle = object
  import Surface as S;
  var a: real;
begin
    a:= S.pos * S.dir;
end Circle;

end Raytracing.
The compiler error message I get is
Zonnon Compiler, Version 1.1.9.0, Friday, January 30, 2009, 12:27:00 PM
(c) 2003-2007 ETH Zurich
C:\Documents and Settings\Frank\My Documents\Lib\Zonnon\Raytracing\raytracing.znn
    [30,9] [0016] The call is ambiguous between the following methods or properties: 'Zonnon.Raytracing.op_Multiply(System.Double, System.Double[])' and 'Zonnon.Raytracing.op_Multiply(System.Double[], System.Double)'

which refers to the multiplication in the body of Circle. Why is it ambiguous?
Logged
rmitin
Moderator
Newbie
*****
Posts: 22


« Reply #1 on: February 09, 2009, 03:23:27 PM »

This is a very interesting example. Compiler is right that gives an error message, however the error message is wrong. It will be fixed later in the next version of the compiler.

The reason is that you imprt definition. Object can implement a definition or import an implementation when threre is no definition. Basically it failed to import the definition, but displayed wrong error message when it tried to find overloaded operator for fields it failed to import.

One correct use would be

Code:
module Main;

type Vector = array 3 of real;

operator '*' (a:real; v:Vector): Vector;
var res: Vector;
begin
  res[0]:= a*v[0];  res[1]:= a*v[1];  res[2]:= a*v[2];
  return res;
end '*';

operator '*' (v:Vector; a:real): Vector;
begin return a*v;
end '*';

operator '*' (v,w:Vector): real;
begin
  return v[0]*w[0] + v[1]*w[1] + v[2]*w[2];
end '*';


implementation Surface;
  var {public} pos, dir: Vector;
end Surface;

type Circle = object
  import Surface as S;
  var a: real;
begin
    a:= S.pos * S.dir;
end Circle;

end Main.

The other

Code:
module Main;

type Vector = array 3 of real;

operator '*' (a:real; v:Vector): Vector;
var res: Vector;
begin
  res[0]:= a*v[0];  res[1]:= a*v[1];  res[2]:= a*v[2];
  return res;
end '*';

operator '*' (v:Vector; a:real): Vector;
begin return a*v;
end '*';

operator '*' (v,w:Vector): real;
begin
  return v[0]*w[0] + v[1]*w[1] + v[2]*w[2];
end '*';


definition Surface;
  var {public} pos, dir: Vector;
end Surface;

type Circle = object implements Surface
  var a: real;
begin
    a:= pos * dir;
end Circle;

end Main.

The error message will be corrected in the next version of the compiler.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!