3 ms·
A protocol is like a set of rules. It it specifies, for example, that each object (a closure) has special bindings, which holds references to its class (which i
by dschiptsov 12y ago
A protocol is like a set of rules. It it specifies, for example, that each object (a closure) has special bindings, which holds references to its class (which is also a closure) and to itself, so it could send a massage to its super or to self. Classes could have its own methods (inner closures).
Basically, when you send a message, you have to specify a symbol, that corresponds to a method name, and parameters. The protocol specifies the "routing", like if there is no method that corresponds to this name, then send (forward) the very same message to your superior (exactly like they do in army, which is an implementation of the actor model).
If supers doesn't know what to do with the message, then there is panic - an exception.
The protocol also specifies the rules of shadowing of symbols, such that a method could be redefined (shadowed) within a closure, so it "overrides" a method with the same name from its ancestors.
Object methods (inner closures) could send messages to classes (invoking class methods) and then doing something with a returned value. Usually, there is some syntactic sugar for that.
In short, inheritance is a set of rules, and these rules could be implemented using various mechanism, like explicit message-passing (a-la Erlang) or implicitly, using procedures calls.
Brian Harvey in classic Berkeley CS61A explained these ideas much better than me.
https://www.youtube.com/watch?v=7u3pp7JCAIU https://www.youtube.com/watch?v=7u3pp7JCAIU
- gclaramunt 12y agoyes, that part makes sense, but in Haskell, an unknown "message" is a compiler error, and bottom represents abnormal termination (e.g exception or even non termination), I don't really understand how that's related to inheritance.
- dschiptsov 12y agoI am not sure that an unknown message could be caught at compile time, because we don't know everything in advance. I also don't think that static type checking is the only way to program. In that toy example I should return an error, because '() is a legit value.