Skip to content

Instantly share code, notes, and snippets.

@fmnoise
Last active May 11, 2017 17:32
Show Gist options
  • Save fmnoise/f3bf1401efc5c8655dcdd66420a74db0 to your computer and use it in GitHub Desktop.
Save fmnoise/f3bf1401efc5c8655dcdd66420a74db0 to your computer and use it in GitHub Desktop.
extending protocol with protocol
; 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