Fork me on GitHub
#malli
<
2022-07-04
>
kokonut03:07:17

Does malli have anything equivalent to Union in Typed Racket? https://docs.racket-lang.org/ts-guide/beginning.html

#lang typed/racket
(define-type Tree (U leaf node))
(struct leaf ([val : Number]))
(struct node ([left : Tree] [right : Tree]))
It is the same concept with F#'s discriminated union. I read malli documentation carefully but couldn't find.

Ben Sless04:07:05

Isn't this just or?

hansbugge08:07:31

Or :multi which has support for discriminators via :dispatch https://github.com/metosin/malli#multi-schemas

👍 2
Ben Sless08:07:46

Is the racket Union discriminating?

kokonut10:07:11

@UK0810AQ2 I believe you can say that.

Ben Sless10:07:11

multi can dispatch to the correct schema which would make it discriminating. If you don't have a way of doing that, you can use or or orn, which is a named union

👍 2
Eric Dvorsak09:07:02

I'm still trying to spec clojure.core with malli https://github.com/clojure/core.specs.alpha/blob/master/src/main/clojure/clojure/core/specs/alpha.clj#L39 I'm trying to find a way to replace this every which specs a map as 3 different kinds of key/values. unfortunately Malli map-of takes only one kind of key I'm wondering if there is a way to transform the values in the schema, so that it checks that it's a map then transforms in a repetitions of tuples. Is it possible with Malli? going through the docs I can't find any clear way

Eric Dvorsak10:07:14

Is it expected that the output of m/parse can be lossy?

(defn -map-like? [x] (or (map? x) (and (seqable? x) (every? (fn [e] (and (vector? e) (= 2 (count e)))) x))))
(def MapLike (m/-collection-schema {:type 'MapLike, :empty {}, :pred -map-like?}))
(m/parse [MapLike [:orn
                                [:some-keyword [:tuple :keyword :any]]
                                [:some-int [:tuple :int :any]]]]
                                   {:k1 1
                                    :k2 2
                                    3 4
                                    5 :six})
{:some-keyword [:k2 2], :some-int [5 :six]}
In this snippet above one can see that because the output of parse is a map, keys of the same tuple are overwritten