how to use plumatic schema to define a function that takes an argument that can be 2 or more different types?

422 Views Asked by At

I can't figure out how to use s/either or s/conditional as part of the input list. Would like to do something like this:

(s/defn parse-int :- s/Int
  [input :- ; either s/Int or s/Str]
    ; if s/Int
    input
    ; if s/Str
    (read-string input)
))
1

There are 1 best solutions below

2
akond On BEST ANSWER
(sc/defn parse-int :- sc/Str
    [input :- (sc/cond-pre sc/Int sc/Str)]
    (if (string? input) "a string" "not a string"))

(parse-int 34545) ; "not a string"
(parse-int "34545") ; "a string"

You could also use either, but it is deprecated.