variant core.typed
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
(ns variants-playground.core | |
(:require [clojure.core.typed :as t :refer [cf defalias ann U Value Str HVec HMap]] | |
[clojure.core.match :refer [match]])) | |
(defmacro Variant [& lst] | |
`(U ~@(for [[tag & items] lst] | |
`(HVec [(Value ~tag) | |
(HMap :mandatory ~@items)])))) | |
(println (macroexpand '(Variant [:foo {:bar Str}]))) | |
;; => (clojure.core.typed/U (clojure.core.typed/HVec [(clojure.core.typed/Value :foo) (clojure.core.typed/HMap :mandatory {:bar Str})])) | |
(cf [:foo {:bar "FizzBuzz"}] | |
(Variant [:foo {:bar Str}])) | |
;; => Internal Error (:<NO LINE>) Incorrect function syntax: [:foo {:bar Str}] | |
(cf [:foo {:bar "FizzBuzz"}] | |
(clojure.core.typed/U | |
(clojure.core.typed/HVec | |
[(clojure.core.typed/Value :foo) | |
(clojure.core.typed/HMap :mandatory {:bar clojure.core.typed/Str})]))) | |
;; .. but works just fine by pasting result from macroexpand. | |
;; => [(HVec [(t/Val :foo) (HMap :mandatory {:bar (t/Val FizzBuzz)} :complete? true)]) {:then tt, :else ff}] |
I wonder if doing it with a regular function would work better?
(defn Variant [& clauses]
(apply U (for [[tag & items] clauses]
(HVec [(Value tag) & items]))))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Macro expansion seems fine, but fails on cf / defalias / etc.. :/