This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Overloading arithmetic


Frederick Ross wrote:
Can kawa handle overloading *, +, etc.?  For instance, if I take the
2d-vector class from the docs, then

(define (+ (a :: <2d-vector>) (b :: <2d-vector>)) (a:add b))

Then using it (or (a:add b) as well) promptly fails with:
Argument #1 (1.0) to '+' has wrong type (gnu.math.DFloNum) (gnu.math.DFloNum)


So the simple minded approach isn't right.  Is there a way to make
this work properly?

Yes and no. There is no general mechanism for operator overloading at this point. There is some support for generic/overloaded functions, but that isn't currently used for the operators like +.

However, you could define <2d-vector> to extend gnu.math.Numeric
or Quantity.  Numeric has lots of abstract methods which you would
have to  implement.  For example the add method implements addition.

You may want to read this carefully:
http://www.gnu.org/software/kawa/internals/numbers.html
(Not fully up-to-date, I suspect, but close enough.)

I don't know how easy this would be, and whether core parts of
Kawa would need to be changed.  Worth an experiment, I guess.
--
	--Per Bothner
per@bothner.com   http://per.bothner.com/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]