https://github.com/lambdaisland/uniontypes
Union Types (ADTs, sum types) built on clojure.spec
https://github.com/lambdaisland/uniontypes
Last synced: 3 months ago
JSON representation
Union Types (ADTs, sum types) built on clojure.spec
- Host: GitHub
- URL: https://github.com/lambdaisland/uniontypes
- Owner: lambdaisland
- Created: 2016-09-24T06:08:41.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-07-24T22:08:07.000Z (over 2 years ago)
- Last Synced: 2025-08-23T21:45:23.235Z (4 months ago)
- Language: Clojure
- Size: 933 KB
- Stars: 115
- Watchers: 8
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
[](https://travis-ci.org/lambdaisland/uniontypes)
[](https://clojars.org/lambdaisland/uniontypes)
# 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:
``` clojure
[lambdaisland/uniontypes "0.3.0"]
```
## Usage
``` clojure
(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