LoginSignup
1
1

More than 5 years have passed since last update.

Clojureで継承っぽいことをする

Last updated at Posted at 2018-05-19

2018/5/23追記:
内容をかみ砕いてまとめました。
Clojureでオブジェクト指向を行うまとめ

Clojureでクラスの継承っぽいことを探してようやく一つの物にたどり着いたので備忘録です。
若干何故動かなかったのか、何故動いたのか分かっていないところも有り。
(エラーメッセージの解読辛い…)

(defprotocol IPerson
    (put[self I])
)
(def MPerson{
    :put(fn[self I]
        (println (str I "は「" (:format-output self) "」です。"))
    )
})
(defrecord Person[name age high format-output])
(extend Person IPerson MPerson)
(defrecord Person2[name age high format-output hobby])
(extend Person2 IPerson MPerson)

(defn make-Person[name age & {:keys [high] :or {high 162.3}}]
    (let [format-output (str "名前: " name " 年齢: " age " 身長: " high "cm")]
        (Person. name age high format-output)
    )
)
(defn make-Person2[name age high hobby]
    (let [format-output (str "名前: " name " 年齢: " age " 身長: " high "cm 趣味: " hobby)]
        (Person2. name age high format-output hobby)
    )
)

(let [
    bob (make-Person "bob" 24)
    shon (make-Person2 "Shon" 53 199.9 "崖登り")
]
    (put bob "僕")
    (put shon "たわし")
)

;僕は「名前: bob 年齢: 24 身長: 162.3cm」です。
;たわしは「名前: Shon 年齢: 53 身長: 199.9cm 趣味: 崖登り」です。
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1