Can core.match be used to add type information for an object?

42 views Asked by At

When I work in Scala, I like that I can pattern match on type and the type-checker will make use of that type:

val x : Any = "boop"
x match {
  case y : String => do-something-stringy(y);
  case z : Int => .... etc
} 

I know in core.typed, a conditional will help the type checker to resolve the exact type. I tried to replicate this using core.match:

(ann do-something-stringy [String -> String])
(defn do-something-stringy [message]
  (str "Hello " message))


;; Doesn't work
(ann do-things [Object -> String])
(defn do-things [foo]
  (match [(type foo)]
         [String] (do-something-stringy foo)
         :else "Nope"))

This fails with an error:

Function do-something-stringy could not be applied to arguments:

Domains: String

Arguments: Object

Ranges: String

with expected type: String

Is there a way to get this to work using core.match?

Thanks!

1

There are 1 answers

0
Ambrose On

Try dispatching on class rather than type. core.typed has no real support for type due to its weirdness around metadata.