refined icon indicating copy to clipboard operation
refined copied to clipboard

Creating a Map of Literal Refined Types as the Key doesn't work

Open bcarter97 opened this issue 2 years ago • 1 comments

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?

bcarter97 avatar Feb 22 '23 11:02 bcarter97

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.

fthomas avatar Feb 26 '23 08:02 fthomas