fthomas / refined

Refinement types for Scala
MIT License
1.71k stars 154 forks source link

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

Open bcarter97 opened 1 year ago

bcarter97 commented 1 year ago

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?

fthomas commented 1 year ago

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.