This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-11-02
Channels
- # announcements (21)
- # babashka (1)
- # beginners (67)
- # calva (18)
- # cider (21)
- # clj-kondo (109)
- # cljs-dev (3)
- # clojure (129)
- # clojure-spec (15)
- # clojure-uk (30)
- # clojurescript (3)
- # datomic (2)
- # graalvm (8)
- # graphql (1)
- # juxt (1)
- # malli (28)
- # off-topic (46)
- # reitit (5)
- # rewrite-clj (33)
- # ring-swagger (2)
- # shadow-cljs (199)
- # vim (9)
I have a question related to malli, but it's probably just an error on my part but I cannot spot it: I defined macro as this (simplified example):
(defmacro x [y]
`(println ~(m/schema [:tuple y])))
And when I call (x int?)
I get:
Syntax error compiling fn* at (/private/var/folders/fm/5mzhclpd7mj0tzjjq796ftc00000gn/T/form-init14721255528686671623.clj:1:1).
Can't embed object in code, maybe print-dup not defined: malli.core$_tuple_schema$reify$reify__4266@2c3b816
However if I define macro as this it works:
(defmacro x [y]
`(println (m/schema [:tuple ~y])))
=> #'clj-rest-client.core/x
(x int?)
[:tuple int?]
I wanted to compute the schema value at macro expansion time to make it faster, but it doesn't seem to work for some reason
maybe someone else can spot it, I've been looking at this for a while with no idea
@roklenarcic macros should return source code, the first example returns a reified protocol instance. You can run (macroexpand ...)
to that
I thought that m/schema does some preprocessing
which would make it faster than sticking simply [:tuple x] in there
btw, it seems that [:tuple]
doesn;'t work
(m/schema [:tuple])
-> :tuple
but (m/schema [:tuple int?])
-> [:tuple int?]
malli has it's own compiler for validation, explain & transform. Reaults of those should be pretty fast.
Creation of schema has some overhead, but if you can store a reference to a transformer, it should be as fast as one can do with Clojure.
thanks for the info
the :tuple
thing... some schemas has a constraint that they should have 1+ childs. I guess tuple doesn't and empty tuple is ok...
I had spec1 code that said -> give me a vector of values and a vector of specs and I will generate a s/fdef
spec (which uses s/cat
), so the code generated a spec like this (s/cat :arg1 arg-spec1 ...)
and potentially (s/cat)
which took an empty sequence just fine. I replaced this with :tuple
from malli and it seems to have a corner case for empty tuple
it works the opposite
(m/explain [:tuple] [])
Execution error (NullPointerException) at malli.core/-tuple-schema$reify$reify$fn (core.cljc:447).
null
but spec with empty (s/cat)
would work fine
yeah I thought so
NPEs are usually not intended
I'm porting my rest client lib from spec to malli now... hopefully I'll have encode/decode soon
I'd like for user to be able to specify the transformation to string I should do before sending any opaque types over the wire
transforms seem to be doing the opposite direction
e.g. if user has LocalDateTime object I'd like for them to specify format in schema that I format it to before I send it...
I'll be finally rid of the nightmare that is trying to conform s/merge
specs