Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simplest way to add a type to a map in clojure

Tags:

clojure

I'm searching for the lightest way to add a type to a clojure map I know i could use records for doing that but i'm wondering if i can do it with regular maps

i've got a function to create a map that represent a person:

(defn person [first-name last-name] {:first-name first-name :last-name last-name})

i would like to be able to do something like that:

(def wayne (person "Wayne" "Shorter"))
....
(type wayne) 
=> person  
like image 743
szymanowski Avatar asked Dec 16 '13 15:12

szymanowski


1 Answers

You could attach some metadata:

(defn person [first-name last-name]
      (with-meta {:first-name first-name :last-name last-name}
                 {:type 'Person}))

(def wayne (person "Wayne" "Shorter"))

(type wayne) ;; Person
like image 191
Kyle Avatar answered Oct 06 '22 01:10

Kyle