Reputation: 12184
I have seen a following piece of code here
(def token-store
(redis-store/->RedisTokenStore secret-key 30 {:pool {} :spec {:host "127.0.0.1" :port 6379}}))
Can anyone explain what does this mean ?
RedisTokenStore is a record that implements TokenStore protocol and its methods. but what does this syntax mean, objects can be created with this syntax as well:
(RedisTokenStore. secret-key 30 {:pool {} :spec {:host "127.0.0.1" :port 6379}})
Then what differently is this symbol -> doing ? Why is it after a user defined namespace ?
I know as a macro, -> is used to
(-> 4 (+ 5)
(- 6))
which translates to (- (+ 4 5) 6) and gives 3. but neither could I google a '->' or type something similar to search what this means.
Upvotes: 0
Views: 149
Reputation: 51470
->RedisTokenStore
is a factory function for record RedisTokenStore
. Clojure defines it automatically when record is defined.
Here is quote from official Clojure datatypes docs:
when a deftype/defrecord Foo is defined a corresponding function ->Foo is defined that passes its arguments to the constructor (versions 1.3 and later only)
So, ->RedisTokenStore
is a helper function which simply calls RedisTokenStore.
constructor. The only difference is that you should import
RedisTokenStore
class to call its constructor explicitly, while ->RedisTokenStore
is an ordinary function and could be require
'ed and then used as a part of redis-store
namespace.
Update: As mattexx poined out in his answer, there is also map->RedisTokenStore
factory function which takes a map as its sole argument.
Upvotes: 6
Reputation: 6606
RedisTokenStore
is declared with defrecord
in the namespace.
At the bottom of the defrecord
doc:
Given (defrecord TypeName ...), two factory functions will be defined: ->TypeName, taking positional parameters for the fields, and map->TypeName, taking a map of keywords to field values.
Upvotes: 7