Thread overview
Overloading by in-conditions?
May 09, 2002
Russ Lewis
May 09, 2002
OddesE
May 09, 2002
Stephen Fuld
May 10, 2002
Russell Borogove
May 09, 2002
OddesE asked the question:
    What is the square root of -4?

It got me thinking...it would be cool to have 2 versions of sqrt, one that returns floats, and the other that reports complex numbers.  You of course could differentiate between them by giving two names, but what if you could just specify different in conditions?

    double sqrt(double val)
    in {
        assert(val >= 0);
    }
    body { ... };
    complex sqrt(complex val)
    in {
        assert(val < 0);
    }
    body { ... };

You can see that the in conditions don't overlap, so it's totally clear which version needs to be used, given any input value.  Most times, compiler will be able to choose the proper version at compile time.  In cases where it can't, then it can put in a runtime check, along with implicit casts from double to float as necessary.

Thoughts?

--
The Villagers are Online! villagersonline.com

.[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
.[ (a version.of(English).(precise.more)) is(possible) ]
?[ you want.to(help(develop(it))) ]


May 09, 2002
"Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3CDAE6AD.712A3C1D@deming-os.org...
> OddesE asked the question:
>     What is the square root of -4?
>
> It got me thinking...it would be cool to have 2 versions of sqrt, one that returns floats, and the other that reports complex numbers.  You of course could differentiate between them by giving two names, but what if you could just specify different in conditions?
>
>     double sqrt(double val)
>     in {
>         assert(val >= 0);
>     }
>     body { ... };
>     complex sqrt(complex val)
>     in {
>         assert(val < 0);
>     }
>     body { ... };
>
> You can see that the in conditions don't overlap, so it's totally clear which version needs to be used, given any input value.  Most times, compiler will be able to choose the proper version at compile time.  In cases where it can't, then it can put in a runtime check, along with implicit casts from double to float as necessary.
>
> Thoughts?
>
> --
> The Villagers are Online! villagersonline.com
>
> .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
> .[ (a version.of(English).(precise.more)) is(possible) ]
> ?[ you want.to(help(develop(it))) ]
>

At the moment the contract checks (in and out conditions)
are removed from release builds. And what if the conditions
do overlap? Mmm I'm not sure about this one....
Better give them different names I think.

--
Stijn
OddesE_XYZ@hotmail.com
http://OddesE.cjb.net
_________________________________________________
Remove _XYZ from my address when replying by mail




May 09, 2002
"Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3CDAE6AD.712A3C1D@deming-os.org...
> OddesE asked the question:
>     What is the square root of -4?
>
> It got me thinking...it would be cool to have 2 versions of sqrt, one that returns floats, and the other that reports complex numbers.  You of course could differentiate between them by giving two names, but what if you could just specify different in conditions?
>
>     double sqrt(double val)
>     in {
>         assert(val >= 0);
>     }
>     body { ... };
>     complex sqrt(complex val)
>     in {
>         assert(val < 0);
>     }
>     body { ... };
>
> You can see that the in conditions don't overlap, so it's totally clear which version needs to be used, given any input value.  Most times, compiler will be able to choose the proper version at compile time.  In cases where it can't, then it can put in a runtime check, along with implicit casts from double to float as necessary.
>
> Thoughts?

Well, if you thought there was a possibility of the argument to a square root function being negative, you could cast the integer or real into a complex.  This seems sort of like casting an integer into a real before division if you want the fractional part.

--
 - Stephen Fuld
   e-mail address disguised to prevent spam


May 10, 2002
Russ Lewis wrote:
> OddesE asked the question:
>     What is the square root of -4?
> 
> It got me thinking...it would be cool to have 2 versions of sqrt, one
> that returns floats, and the other that reports complex numbers.  You of
> course could differentiate between them by giving two names, but what if
> you could just specify different in conditions?

I gots two words for ya:

"unsigned float"

/me runs and hides