Liking cljdoc? Tell your friends :D

Build Status Clojars Project

lambdaisland/uniontypes

Union Types (Algebraic Data Types) for Clojure and ClojureScript, based on clojure.spec.

Provides a case-of macro, which does case matching based on which branch in an or spec a value conforms to.

case-of checks at compile time that all cases are handled, and if not throws an exception.

Installation

To install, add the following dependency to your project or build file:

[lambdaisland/uniontypes "0.2.0"]

Usage

(require '[lambdaisland.uniontypes :refer [case-of]])

(s/def ::availability (s/or :sold-out  #{:sold-out}
                            :in-stock  pos-int?
                            :reordered (s/tuple pos-int? pos-int?)
                            :announced string?))

(defn display-status [availability]
  (case-of ::availability availability
    :sold-out _
    "Sold out."

    :in-stock amount
    (str "In stock: " amount " items left.")

    :reordered [min max]
    (str "Available again in " min " to " max "days" )

    :announced date
    (str "Available on " date ".")

    :spec/invalid msg
    (str "Not a valid availability: " msg)))

License

Copyright © 2016-2017 Arne Brasseur

Distributed under the Mozille Public License 2.0

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close