I think it's also important here that Clojure methods are actual Java
methods - Clojure likes to stay close to the host functionality. A map
with a function isn't a class with a method because the JVM bytecode
doesn't let you invoke it that way directly. A Clojure function is not
a method because methods are not first class, it's an Object with a
static .invoke method (and some other details that are less important
here).

This object as hash with functions model is useful for learning (and
there's a lot of scheme and common lisp code that actually works this
way). With some effort, you can do everything with a function in a map
that you can with an object with a method if you are careful to
provide the object of the first arg.

But it's normal in Clojure to use the built in affordances. For real
code, defrecord gives you a new class that operates in a way similar
to this but with lower friction to the underlying bytecode (it
implements actual methods but also acts like a hash map for its
fields). Protocols let you use a hash map for extension (they act like
interfaces for classes, but you can add new implementations
dynamically, letting you do what you would do by adding a function
under a key on a hash, but tracking it on the protocol side).

On Tue, Mar 31, 2020 at 7:39 AM Jason Felice <jason.m.fel...@gmail.com> wrote:
>
> A subtle difference between a map of functions and a Python class is that the 
> class has implicit "self" or "this".  Otherwise, these are semantically the 
> same.  Well, ignoring that Clojure maps are immutable.
>
> In fact, C++ compilers compile methods by inserting a first "this" argument 
> and mangling the name.  Virtual methods (ones which can be overridden) are 
> collected into what is effectively a map attached to the instance.
>
> On Tue, Mar 31, 2020 at 4:41 AM Dieter Van Eessen 
> <dieter.van.ees...@gmail.com> wrote:
>>
>> Hello,
>>
>> I've got a clojure and a python piece of code. Both seem to create what can 
>> be considered an instance of a class. Wherein lies the conceptual difference?
>>
>> Python:
>> class MYCLASS():
>>     def __init__(self, x):
>>           self.x = x
>>     def MYMETHOD(self):
>>          ...
>>
>> def MYFUNCTION():
>>     lol = MYCLASS()
>>
>> Clojure:
>> (defn MYCLASS [x]
>>      {:x [x]
>>       :MYMETHOD (fn [] (MYCLASS ...))})
>>
>> (let [lol (MYCLASS ...)])
>>
>> I know its not valid code, but I hope you see what I'm aiming at: isn't 
>> using a map with functions in it just the same as a class?
>> Or is only the user interface of the language conceptually equal, while the 
>> underlying plumbing is completely different?
>> If this is the case, wherein lies the major differences?
>>
>> If one could simply point me in the right direction, I'd already be very 
>> pleased. Most literature I've read so far only explains clojure can be used 
>> this way, but never focuses deeper on the subject.
>>
>> kind regards,
>> Dieter
>>
>> --
>> 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.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/clojure/49cd26ca-48ae-48b4-a5ab-b0f7c711b77e%40googlegroups.com.
>
> --
> 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.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/clojure/CAB6_SoYCTeKi2teO%3DbgyEB-Vu3C-fCGpX6T%3DXZB8ApmDP-c8vA%40mail.gmail.com.

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/CAGokn9KXN7oB05NUSX9GfNO9F7MOOo1_m5fj5my12ioRMrN3FQ%40mail.gmail.com.

Reply via email to