scalarefinement-type

AutoRefineV not picking up explicit inference from Map?


I have a refined type definition like this:

type D = String Refined Regex "(a|b)"

I can use the refinement in a single line expressing the value but for some reason autoRefineV is not picking it up when used in the context of a Map, even if the type is explicit. For example:

import eu.timepit.refined.auto.autoRefineV
val test1: D = "a" // this is fine
val test2 = Map[D, String]("a" -> "test") // this fails
val test3: Map[D, String] = Map("a" -> "test") // this also fails
val test4 = Map(test1 -> "test") // this works!

The error I'm getting is:

found : (String, String)
required : (D, String)

So for some reason, the value is not being auto-converted from a string to a D type? Do I need to define a refined object of type Map[D, String]? Do I need to use a witness (which I have no idea how to use)?


Solution

  • You have an implicit conversion from String to D, but definitions of test2/3 would need one from (String, String) to (D, String) which doesn't exist.

    val test5 = Map(("a": D) -> "test")
    

    should work if you just want to write it in one line.