Search code examples
clojureplumatic-schema

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


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)
))

Solution

  • (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.