Last active
May 11, 2017 17:32
-
-
Save fmnoise/f3bf1401efc5c8655dcdd66420a74db0 to your computer and use it in GitHub Desktop.
extending protocol with protocol
This file contains 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
; inspired by example from "Clojure Applied" | |
; book says that it's not possible | |
(ns sales.core) | |
(defprotocol Cost | |
(cost [this])) | |
(defrecord Product [price quantity] | |
Cost | |
(cost [this] | |
(* (.price this) (.quantity this)))) | |
(defprotocol SaleCost | |
(sale-cost [this])) | |
(extend-protocol SaleCost | |
sales.core.Cost | |
(sale-cost [this] | |
(* (cost this) 0.75))) | |
(cost (->Product 100 2)) ; => 200 | |
(sale-cost (->Product 100 2)) ; => 150 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment