Why not trying it right away in the REPL?

user=> (defrecord Bar [state]) ; <- not implementing IFn
> user.Bar
> user=> ((->Bar "foo")) 


> ClassCastException user.Bar cannot be cast to clojure.lang.IFn 
>  user/eval15532 (form-init5689008917050406381.clj:1)
> user=> (defrecord Foo [state] clojure.lang.IFn (invoke [this] (println "my 
> state is" state)))
> user.Foo
> user=> ((->Foo "foo"))
> my state is foo
> nil

user=> ((->Foo "foo" "bar"))
> ArityException Wrong number of args (2) passed to: 
> user/eval15486/->Foo--15498  clojure.lang.AFn.throwArity (AFn.java:429) 


Here you have it: implementing IFn is enough to use a record as a function. 
You can then implement just the arities you're interested into.

Cheers,
c.

Il giorno giovedì 14 maggio 2015 21:32:37 UTC+2, piast...@gmail.com ha 
scritto:
>
> The docs offer this example: 
>
> https://clojuredocs.org/clojure.core/defrecord
>
> user=> (defrecord Someone [nick-name preffered-drink] Fun-Time (drinky-drinky 
> [_] (str nick-name "(having " preffered-drink "): uuumm")))
> user.Someone
>
> user=> (def dude (->Someone "belun" "daiquiri"))
> #'user/dude
>
> user=> (drinky-drinky dude)
> "belun(having daiquiri): uuumm"
>
> But if a record implements clojure.lang.IFn it can be called directly? Do I 
> understand this correctly? When I run macroexpand-all on defrecord-ifn, as 
> defined here: 
>
> https://github.com/overtone/overtone/blob/e200075da27375727db1f5ce342e2e1c22ea1dbd/src/overtone/helpers/lib.clj
>
> I see that it extends clojure.lang.IFn as in: 
>
> clojure.lang.IFn 
> (invoke [this__1446__auto__] 
>   ((fn [this & args] 
>     (apply synth-player sdef params this [:tail instance-group] args)) 
> this__1446__auto__)) 
> (invoke [this__1447__auto__ arg0] 
>   ((fn [this & args] 
>     (apply synth-player sdef params this [:tail instance-group] args)) 
> this__1447__auto__ arg0)) 
> // etc, all 20 arities are defined
>
> So, that means I could call the record as a function? 
>
>
>

-- 
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/d/optout.

Reply via email to