A few years ago I built some kind of internal DSL using Clojure macros
which allowed to create objects and classes like in conventional OOP
languages as Smalltalk and Java.
The macro obj creates a classless, immutable object, for example:
(obj {:x 1, :y 2} {:f (fn [] (+ (self :x) (self :y)))})
obj expects to maps as arguments, one for the instance variables (:x and
:y) and one for the methods (:f). The variable self is provided by obj
automatically.
The methods can be activated using a message passing style:
((obj {:x 1, :y 2} {:f (fn [] (+ (self :x) (self :y)))}) :f) ;=>3
Classes can be created through an explicit abstraction step by sending the
message :kappa to an object:
((obj {:x 1, :y 2} {:f (fn [] (+ (self :x) (self :y)))}) :kappa 'C) ;=>
#'dosl2clj.core/C
Classes understand the message :new for creating new instances:
((C :new) :data) ;=> {:x 1, :y 2}
All objects understand the message :data. Class hierarchies are possible,
too.
I built the DSL, because I wanted to figure out, wether it could make
porting an application from Smalltalk to Clojure less difficult. I tried
two simple applications.
My conclusion is: Yes, the porting is possible, of course, and quite
simple. But it is in no way simpler than using the native Clojure language
features. Using normal hash-maps and functions isn't more difficult.
Building a DSL for objects isn't worth the effort.
Johannes
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/clojure/51957f2f-ecf3-4bf0-b1de-de1968baac11%40googlegroups.com.