> So I scraped the rust off my computer science education and thought a
> bit about the differences between functional and OO languages.
>
> One of the nice things about languages like Lisp/Prolog/Ocaml(?) is that
> you can pass functions around by reference and composite them into other
> functions. This allows for some problems to be very elegantly solved, in
> particular what in OO is called the Strategiy pattern. Also, from what I
> recall from academia its very useful for the type of substitutions used
> in language parsing based on a BNF definition (my knowledge is not very
> deep here).
>
> In contrast, strict OO languages like Java do not offer this
> possibility. You can't pass a function directly, so instead you have to
> pass an object that conforms to an Interface. You are forced to using a
> pattern.
>
> Interestingly, AS2 seems to meet in the middle. While not quite as
> syntactically elegant as Lisp or Prolog, you can pass functions around
> by reference. This would allow for an implementation of Strategy that is
> probably a lot more succint in Java (simply pass the function), although
> this function would assumedly be loosely typed. From what I understand
> - we are going to lose this flexibility in AS3.
What you're talking about are "First Order Functions". It means
functions are values just like an integer or an object. It means also
you can store them and pass them around. This is actually the main
property of so-called "Functional languages".
Please note that in a pure Lambda Calculus model, everything is a
function. Just like in a pure OO model everything is an object. However
today efficient Functional Programming Languages such as Lisp OCaml or
Haskell are not "pure" to this point. (OCaml even have a pretty powerful
class type system).
In AS2, you can store functions but you cannot express the type of a
function (only using the type Function, which does not give you a lot of
informations about it). Also, methods are not closures, so they're
actually independant from the instance they're used with.
var f : Function = o.myMethod;
f(); // call without "o" context
This is what was changed with AS3, where you can actually do that and it
will work. That opens a new way of defining events where you can simply
write :
mc.onRelease = o.mymethod;
The only problem left are the "types" of a function. Since obviously you
don't want to assign a function taking 1 paremeter to a function needing
4 , and having different types.
I'm not sure if AS3 allows the following :
mc.onRelease = function(a : String,b : String ) : String { return "" }
But it surely shoudln't :)
Back to functional languages, you can express type function using the
"->" notation :
var onEvent : Event -> Void
means "takes an Event paremeter and returns Void", while :
var onEvent : Event -> String -> String -> Boolean
means "takes 3 parameters Event String and String and returns a Boolean".
So you can actually strictly-type your functions which is pretty good
news. And BTW all of that will be in "haXe".
Nicolas
_______________________________________________
osflash mailing list
[email protected]
http://osflash.org/mailman/listinfo/osflash_osflash.org