Anonymous user
Delegates: Difference between revisions
Adds Clojure solution
(Added Sidef) |
(Adds Clojure solution) |
||
Line 445:
}
</lang>
=={{header|Clojure}}==
<lang clojure>(defprotocol Thing
(thing [_]))
(defprotocol Operation
(operation [_]))
(defrecord Delegator [delegate]
Operation
(operation [_] (try (thing delegate) (catch IllegalArgumentException e "default implementation"))))
(defrecord Delegate []
Thing
(thing [_] "delegate implementation"))</lang>
{{out}}
<pre>
; without a delegate
=> (operation (Delegator. nil))
"default implementation"
; with a delegate that does not implement "thing"
=> (operation (Delegator. (Object.)))
"default implementation"
; with a delegate that implements "thing"
=> (operation (Delegator. (Delegate.)))
"delegate implementation"
</pre>
=={{header|CoffeeScript}}==
{{trans|Python}}
|