News for hackers. Stuff that matters.
Re:Inheritance Is Terrible by lispm on Saturday March 19, 2016 @05:30PM (#11320556)
Basically CLOS (Common Lisp Object System) implements Flavors' mixins as normal classes. Example:

    (defclass priceable-mixin () ()
      (:documentation "we can calculate a price for something"))


    (defclass standard-priceable-mixin (priceable-mixin)
      ((price :initarg :price)))

    (defmethod price ((i standard-priceable-mixin))
      (slot-value i 'price))


    (defclass shared-priceable-mixin (priceable-mixin)
      ((royalty :initarg :royalty)
       (markup  :initarg :markup))
      (:documentation "the price has two components: royalty and markup"))

    (defmethod price ((i shared-priceable-mixin))
      (with-slots (royalty markup) i
        (+ royalty markup)))


    ; we want to sell books and movies

    (defclass book (shared-priceable-mixin item)
      ()
      (:default-initargs :royalty 2 :markup 8))

    (defclass movie (standard-priceable-mixin item)
      ()
      (:default-initargs :price 10))


    ; there is a cart with items and a total price

    (defclass cart ()
      ((total :initform 0   :accessor cart-total)
       (items :initform nil :accessor cart-items)))

    (defmethod add-item-to-cart ((i item) (c cart))
      (push i (cart-items c))
      (incf (cart-total c) (price i)))

There are no comments on this comment yet.