Hi Krukow,

Krukow wrote:
> SinDoc wrote:
>> I was wondering if someone could point me to recent usage examples of
>> deftype, defrecord, and reify. Reading [1] helped a lot but it wasn't
>> particularly easy to find it since it's not linked from the sidebar.
> 
> Blog: http://blog.higher-order.net/2010/05/05/circuitbreaker-clojure-1-2/
> 
> Code: http://github.com/krukow/clojure-circuit-breaker

This is a good example. I like the way merge is used to avoid redundancy while 
dispatching methods. Deftype is not being used in the example but if I want the 
map, I should go for defrecord anyway.

The only member data _I'm_ able find are the ones that are passed to the 
default constructor, namely at the time that the abstraction is reified. What 
if I'd have to give create a member field that is not necessarily known by the 
caller or instantiator. That is, it's the abstraction itself that has to create 
and maintain the value of that field.

One of the abstraction that I was hoping to implement in Clojure is a 
Scheme-like pair in order to demonstrate various memory management techniques. 
Once we do (cons a b), an abstract pair should be made that only contains a 
pointer to its location in memory: (tag address). Here's the pair 
implementation [2] in Scheme R5RS.

I'm thinking optional parameters could be used. Since callers don't have to 
provide values for them, I can use them to maintain the state for each 
instance. I might be missing the obvious here since these are data types and 
there should be an explicit way to say that this _thing_ is my data.

>> Specifically, what I'd like to know is:
>>
>>  - How to define and access member data fields -also mutable in case
>> of deftype- to my ADTs.
> 
> The example is there for immutable fields. Why do you need mutability?

I don't need it. Just wanted to see some examples. I'd rather go immutable 
anyway ;)

>>  - Whether I can refer to type instances -an equivalent to the 'this'
>> keyword in Java.
> 
> The first argument to a protocol function corresponds to the "this"
> keyword in Java, e.g.,
> 
> (extend ClosedState CircuitBreakerTransitions
>   ...
>  :on-success
>    (fn [{f :fail-count p :policy, :as s}] ;; note we can destructure
> the 'this' argument (s)
>      (if (zero? f) s (ClosedState. p 0)))
> ...)

Nice!

>>  - How to define constructors.
> 
> A single constructor is automatically defined for you. In my case with
> two params:
> 
> (ClosedState. policy fail-count)
> 
> If you need more flexibility, I believe you need gen-class, but I am
> unsure.

I can live with one constructor for now.

>> [1]http://clojure.org/datatypes
[2] http://is.gd/ctoSg

> Hope that helps. Kind Regards,

Thank you very much, Krukow. It sure did help.

Kind regards,
SinDoc

-- 
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

Reply via email to