Re: Issue of the week

new topic     » goto parent     » topic index » view thread      » older message » newer message

On Wed, 30 Sep 1998, Hawke wrote:

[Snip my code. My messages are becoming too long these days. Sorry folks.]

> i'm not sure i understand this... or, at the very least,
> i wasn't aware that builtin functions could be overloaded...
>
> the only way i can think of to attempt to determine if
> i understand this, and it's mechanism, it to throw
> an example back, to see if i can apply what i have learned
> (or thought i learned) to a problem of practical nature.
>
> a while back, i observed that X*X worked (oftenly)
> much faster than power(x,2).
> so:
> function oldpower(object x, object y)
>   return power(x,y)
> end function
>
> without warning
> function power(object x, object y)
>    if int(y) then
>         --fast squares
>         if y=2 then return x*x   end if
>         --fast cubes
>         if y=3 then return x*x*x end if
>    end if
>    return oldpower(x,y)
> end function
> with warning

This method *isn't* overloading as such. Not like in C or Ada anyway.
It's redefinition. In other languages, it'd be interpreted (or compiled :)
as mutual recursion, which Euphoria doesn't handle this way.

>  ...overloading gives me headaches...
>    "but! but! but!, that '*' doesn't _really_ mean
>     multiplication!")

I like overloading. I think it's useful, but yes, it *is* possible to
misuse it. Take the following Ada Code:

type Dodgy is new Integer;
-- Have to define a new type 'cause Int*Int->Int is already defined!

function "*"(Left: Integer; Right: Integer) return Dodgy is
begin
    return Dodgy(Left + Right); -- *Plus*?! Oh dear...

    -- read Dodgy() as "make_into_type_Dodgy" rather than
    -- "is_of_type_Dodgy". Ada =/= Euphoria remember!

end Dodgy;

--
Carl R White
E-mail...: cyrek- at -bigfoot.com -- Remove the hyphens before mailing. Ta :)
Url......: http://www.bigfoot.com/~cyrek/
"Ykk rnyllaqur rgiokc cea nyemdok ymc giququezka caysgr." - B.Q.Vgesa

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu