Hi Max, a happy new year to you and everybody!
Sorry for the late reply, I was somewhat busy over the holidays.
On 12/27/2011 11:29 PM, max.wolf wrote:
> It was a pleasent surprise to see "interfaces" introduced in pure
> 0.50.
> I think it is a nice step forward.
> Now i wonder if it would be a good idea to have type declarations for
> functions as well.
I left this out on purpose. For the purpose of defining a type via a
description of its interface, the result type of a function isn't really
needed anywhere.
> Something like:
> public factorial::number;
> with the semantics that an exception is thrown by the runtime in case
> the computed value doesn't match the type declaration.
A simple approach like this won't fly in Pure since many functions are
polymorphic. So we'd need to add a lot of syntactic baggage to the
language to just declare something like "this function returns type x if
applied to such and such arguments".
But there's already an easy way to get that with ordinary Pure code.
Just define yourself an 'assert' function, e.g., like this:
assert p x = if p x then x else throw (bad_assert p x);
Then you can write, e.g.,
factorial n = assert numberp (if n>0 then n*factorial (n-1) else 1);
An alternative would be to define 'assert' as a macro that could
conditionally be optimized away at compile time if not wanted, similar
to how C's 'assert' works:
//#! --disable assertions
#! --if assertions
def assert p x = if p y then y else throw (bad_assert p y) when y = x end;
#! --else
def assert p x = x;
#! --endif
Cheers,
Albert
--
Dr. Albert Gr"af
Dept. of Music-Informatics, University of Mainz, Germany
Email: Dr.Gr...@t-online.de, a...@muwiinfa.geschichte.uni-mainz.de
WWW: http://www.musikinformatik.uni-mainz.de/ag