Xref: utzoo comp.lang.eiffel:562 comp.object:705 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!uakari.primate.wisc.edu!aplcen!haven!adm!cmcl2!lanl!lambda!jlg From: jlg@lambda.UUCP (Jim Giles) Newsgroups: comp.lang.eiffel,comp.object Subject: Re: Real,Complex,Inheritance and Subtyping Message-ID: <14187@lambda.UUCP> Date: 10 Jan 90 04:30:37 GMT References: <1526@castle.ed.ac.uk> Lines: 50 From article <1526@castle.ed.ac.uk>, by db@lfcs.ed.ac.uk (Dave Berry): > [...] Are there > any languages in which it is possible to ensure that Real + Real calls > Real.+ and any combination of Reals and Complexes calls Complex.+ , without > having to define all cases explicitly? NEMESIS is an experimental language here which can do that. Although not explicitly intended to be Object Oriented, NEMESIS has several features which make generic funcions (at least) easier to write and use. The declaration of complex plus '+' might look something like this: Class Number is (integer, real, complex) Infix commutative associative operator '+' is Complex inline function Complex_Plus(Number::x, Complex::y) Complex_Plus.real = complex(x).real + y.real Complex_Plus.imag = complex(x).imag + y.imag End Complex_Plus Since NEMESIS is based (loosely) on Fortran, the declaration syntax for variables and functions puts the typename first. Class isn't like class in OOP, it is just a way of giving a name to a set of data types so that generic programs can be coded more simply. Note that the degree to which the function is polymorphic is bounded since only those types in the definition of Number are allowed as operands. This may sometimes be useful. If you disagree, then the first operand (x) could have been declared as Object instead of Number and then any type operand would be permitted. There is a certain danger in this because Complex(x) must also accept whatever type is passed, and the result must have .real and .imag components, and those components must be able to be added to reals. Note that the operator is declared as commutative so that the non-complex operand will always be matched to the first argument to the function. If both operands are complex, the order of operands is irrelevant (the compiler assumes this). The complex(x) function is also generic and is the no-op if x is already complex. The function will be expanded inline (provided the operand types are known at compile time and that the module containing the complex '+' definition is available to the compiler). If late binding must occur, the function will be called as an external. The above is not actually the declaration of Complex in NEMESIS since there are more Number types than those given and there are more attributes that operators and functions can be given. Still, this is the bare bones of the matter. J. Giles