[Edu-sig] Quadrays again... cross-training in two langauges
Mark Engelberg
mark.engelberg at gmail.com
Mon Aug 10 21:02:18 CEST 2015
>
> The idea of an interface has migrated to that of a "protocol" which
> provides a ready namespace for a "record".
>
A protocol is more than just an "interface by a different name" or an
"interface+namespace".
To understand the significance of a protocol, you need to first understand
the "expression problem". The expression problem asks you to consider a
set of polymorphic functions over multiple datatypes. Without controlling
or being able to recompile the original source code, can you easily extend
these functions to a new datatype, and can you easily create a new
polymorphic function across those datatypes?
Typically, OO languages make it easy to add a new datatype (just implement
all the methods you want to implement for the new datatype), but hard to
add a new method to an object whose source code you can't recompile.
Typically, FP languages make it easy to add a new function (just take cases
on the datatype in the body of the function), but no good way to add a new
case for a new datatype to a function whose source code you can't recompile.
Clojure has *two *clean, elegant solutions to the expression problem:
protocols and multimethods. Protocols, for example, even let you implement
your own new, custom "methods" on Java's built-in string datatype, which is
what makes them far more powerful than interfaces. (Look up
extend-protocol and extend-type for more info).
Most languages don't have a solution to the expression problem. Some claim
they have a solution because you can choose what sort of extensbility you
are likely to need (am I more likely to add new datatypes or new
functions?), and code accordingly using either an FP or OO approach.
Python roughly falls into this category, but this is not a true solution to
the expression problem. A true solution in Python involves writing your
programs using the OO approach, and then google for "Python monkey
patching" to look up tricks to inject new methods into classes you don't
control -- it's possible, but generally frowned upon... not particularly
good style in Python or a design principle we would want to teach students.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20150810/df10ded2/attachment.html>
More information about the Edu-sig
mailing list