refined
refined copied to clipboard
Creating a Map of Literal Refined Types as the Key doesn't work
Hi, I've noticed if I try to create a Map with the key as a Refined Type and instantiate it with a literal, the compiler gives an error:
import eu.timepit.refined.auto._
import eu.timepit.refined.types.all.NonEmptyString
val bar: Map[NonEmptyString, NonEmptyString] = Map("1" -> "foobar")
/*
found : (String, String)
required: (eu.timepit.refined.types.all.NonEmptyString, eu.timepit.refined.types.all.NonEmptyString)
*/
I can change to just the value as a Refined type and it works as expected:
import eu.timepit.refined.auto._
import eu.timepit.refined.types.all.NonEmptyString
val bar: Map[String, NonEmptyString] = Map("1" -> "foobar")
// val bar: Map[String,eu.timepit.refined.types.all.NonEmptyString] = Map(1 -> foobar)
I can also create a Map out Tuples which works:
import eu.timepit.refined.auto._
import eu.timepit.refined.types.all.NonEmptyString
val bar: Map[NonEmptyString, NonEmptyString] = Map(("1", "foobar"))
// val bar: Map[eu.timepit.refined.types.all.NonEmptyString,eu.timepit.refined.types.all.NonEmptyString] = Map(1 -> foobar)
So it looks like the Arrow syntax is causing this - is this intended behaviour?
So it looks like the Arrow syntax is causing this - is this intended behaviour?
Not intended but expected at least. The arrow syntax is an implicit conversion and the automatic refinement is an implicit conversion but Scala won't chain them so that your first snippet typechecks.