It's perfectly fine to use dosync/alter within a defrecord function. Couple 
other things:
- alter takes a fn and then the args but without parens
- you can (and should) refer to fields of a record directly within the 
defrecord functions, so this is simpler:
- withdraw was misspelled
- withdraw should subtract money, not add it

(defrecord bankAccount [balance holder]
  IBankAccount                 
  (deposit [this amount] (dosync (alter + balance amount)))
  (withdraw [this amount] (dosync (alter - balance amount))))

However, I would suggest that in most cases I would not create an interface 
for this purpose (I'm guessing you're coming from an OO background). And I 
would also consider making the bank account simply data without state and 
instead wrapping the state around the data.

;; just data, just values
(defrecord BankAccount [balance holder])

;; deposit takes an account and modifies it to return a new account
;; as much as possible, just write functions that take and return values
(defn deposit [bank-account amount]
  (update bank-account :balance + amount))

;; same for withdraw
(defn withdraw [bank-account amount]
  (update bank-account :balance - amount))

;; then if you need a stateful bank account, wrap that state around the data
(def checking (ref (->BankAccount 1000 "Alex")))
(def savings (ref (->BankAccount 500 "Alex")))

;; and maybe create some functions that work specifically on stateful 
entities
(defn transfer [from to amount]
  (dosync
    (alter from withdraw amount)
    (alter to deposit amount)))
   
(transfer checking savings 200)
@checking  ;; #user.BankAccount{:balance 800, :holder "Alex"}
@savings  ;; #user.BankAccount{:balance 700, :holder "Alex"}




On Wednesday, October 21, 2015 at 6:45:23 AM UTC-5, Alex Floor wrote:
>
> Hello all,
>
> I am currently studying Clojure and wanted to make a bankaccount with 
> reference, so I came up with the following approach:
>
> (defprotocol IBankAccount
>      (deposit [this amount])
>      (widthraw [this amount])
>      )
>
>
> (defrecord bankAccount
>                  [balance holder]
>                  
>                  ;implements the IBankAccount
>                  IBankAccount
>                  
>                  (deposit [this amount] (dosync (alter (+ (:balance this) 
>  amount))))
>                  (widthraw [this amount] (dosync (alter (+ (:balance this) 
> + amount))))
> )
>
> And the balance needs to be a ref, I think I misunderstood somethingh but 
> couldn 't find a clear answer about the use of dosync -> alter within a 
> defrecord.
>
>
>

-- 
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].
For more options, visit https://groups.google.com/d/optout.

Reply via email to