[Prev][Next][Index][Thread]
Re: Dynamic typing confuses me!
Bertrand Augereau wrote:
>
> Hello, I'm coming from a traditional strongly-typed background (C++ & Ada95)
> and I'm trying to get a grasp on modern functional objects.
> Why does this code give a run-time error?
>
[code deleted]
> ...if I give it all the typing info, it works but it seems to me it can
> infer it in theory.
> Am I completly misunderstanding?
>
> BTW I'm using functional developer...
In theory it can infer that you only have one call to fact, and that call passes an integer, so it could compile the fact method as though you had specified x :: <integer>. I guess the compiler isn't that smart yet.
By the way, did you try using Color Dispatch Optimizations before and after adding the <integer> type declarations to the parameter and return value? It's kinda cool. (Just don't let it make you optimize your code too early. :-)
I tried the posted code in "production mode" in FunDev and indeed it doesn't give a compile time warning. It should.
As a side note, the indentation for your code was a little odd. (Perhaps that was an artifact of posting to the newsgroup?) A more common way would be like this, with the body of the method indented a bit:
define method fact (x :: <integer>) => (f :: <integer>)
if (x = 1)
1
else
fact(x - 1) * x
end
end method fact;
define method main () => ()
fact (5);
end method main;
Follow-Ups:
References: