On Mon, Aug 10, 2015 at 12:58 PM, Kirby Urner <kur...@oreillyschool.com> wrote:
> Clojure, like Java, is not supportive of operator overloading right? I > can't go like: > > (defprotocol VectorOps > (__add__ [this other]) > (__sub__ [this other]) > (len [this]) > (__neg__ [this]) > (norm [this])) > > ... and then use (+ A B) to add two vectors, or (- A) to negate one. > Python is very interesting in the way that its main arithmetic operators dispatch to specific methods that you can override to achieve a certain degree of operator overloading. For performance reasons, clojure.core/+ isn't something you can directly overload. However, you can replace it in your own namespace with a `+` that has a more complex polymorphic dispatch process and then do whatever you want. For a real-world example of overloading + in this manner to work on vectors and matrices, see: https://github.com/mikera/core.matrix > > I think the basics of Polymorphism may be communicated without introducing > the wrinkle of not having original source code. Not wanting to change it > might be easier for a math student to understand. > > Not having the original source code sounds like a social problem, an > institutional issue, vs. a truly theoretical issue, and that's confusing to > newbies. > I see it as a more fundamental theoretical issue, and relevant to students in the sense that one of the higher aims of software engineering is to figure out how to deliver reusable pieces of code. Even if all the code in the world were publicly available on Github, it would be folly to teach that reuse involves forking and editing code. The expression problem is one meaningful measure of whether your programming language forces you to do that, when what you really want to do is build off of some component in a way that doesn't involve editing the original. This is a sufficiently important principle that we need to take care on how we teach it to students in programming languages that don't have robust support for both axes of function and datatype extensibility.
_______________________________________________ Edu-sig mailing list Edu-sig@python.org https://mail.python.org/mailman/listinfo/edu-sig