Created
August 12, 2015 19:31
-
-
Save tolitius/6642519d4ee9f6b0f819 to your computer and use it in GitHub Desktop.
Plain Old Clojure Object ( http://www.dotkam.com/2015/08/12/plain-old-clojure-object/ )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns poco) | |
(gen-class | |
:name org.stargate.PlainOldClojureObject | |
:state "state" | |
:init "init" | |
:constructors {[Boolean Boolean String] []} | |
:methods [[isHuman [] Boolean] | |
[isFound [] Boolean] | |
[planet [] String]]) | |
(defn -init [human? found? planet] | |
[[] (atom {:human? human? | |
:found? found? | |
:planet planet})]) | |
(defn- get-field [this k] | |
(@(.state this) k)) | |
(defn -isHuman [this] | |
(get-field this :human?)) | |
(defn -isFound [this] | |
(get-field this :found?)) | |
(defn -planet [this] | |
(get-field this :planet)) | |
(defn -toString [this] | |
(str @(.state this))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user=> (import '[org.stargate PlainOldClojureObject]) | |
org.stargate.PlainOldClojureObject | |
user=> (def poco (PlainOldClojureObject. true true "42")) | |
#'user/poco | |
user=> poco | |
#object[org.stargate.PlainOldClojureObject 0x68033b90 "{:human? true, :found? true, :planet \"42\"}"] | |
user=> (.isHuman poco) | |
true | |
user=> (.isFound poco) | |
true | |
user=> (.planet poco) | |
"42" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment