On Sunday, December 29, 2013 10:11:47 PM UTC+1, tbc++ wrote:
>
> Not mentioned in Cedric's post are two other important things:
>
> Protocols can be extended to existing types. For example:
>
> (defprotocol IType
>   (type-as-string [x]))
>
> (extend-protocol IType
>   String
>   (type-as-string [x] "string")
>   Integer
>   (type-as-string [x] "integer"))
>
> => (type-as-string 42)
> "integer"
>
> Here we are adding new methods to "sealed" closed classes that already 
> exist in the JVM. We never modify these classes, we simply extend our 
> protocol to them. 
>
> Secondly, all protocol functions are namespaced. This allows us to extend 
> classes without fear of overwriting existing methods. This then is more 
> powerful than monkey patching in Ruby or Python as the resulting method is 
> more like 42.user_type-as-string(). Clojure's namespace system then allows 
> you to refer to one method or the other just as you would any normal 
> functions. 
>

You're not really adding methods to classes in Clojure. You're just 
defining external functions. Can you make them private or protected? In my 
opinion, the Expression Problem in FP and OOP are two different problems. 
In FP you can "solve" it more easily because you use pseudo-classes which 
don't behave like real classes at all. The proof of this is that any 
sufficiently dynamic OO language can solve the problem the same way Clojure 
does just by using classes with no methods and by using overloading 
resolved at runtime.
Of course, you don't do that in OOP because you want to work with real 
classes. For instance, C# has extension methods which let you "add" methods 
to existing classes without any recompilation. Problem solved? I don't 
think so. Extension methods are just static functions that emulate the 
behavior of instance methods, but without any kind of encapsulation and 
possibility of inheritance.

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to