kenbot / goggles

Pleasant, yet principled Scala optics DSL
MIT License
195 stars 7 forks source link

Indexing is broken for literals #39

Open kenbot opened 7 years ago

kenbot commented 7 years ago

When a literal is interpolated in an index position, the type is overspecialised to the exact literal instance type (Int(1), String("abc"), Char('a') etc), rather than the expected type (Int, String. Char).

I believe the problem lies in the code that parses the implicit monocle.function.Index[S,I,A] instance.

Example:

scala> val x = Map("a" -> 1, "b" -> 2, "c" -> 3)
x: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2, c -> 3)

scala> get"$x[${"a"}]"
<console>:29: error: No implicit monocle.function.Index[scala.collection.immutable.Map[String,Int], String("a"), _] found to support '[]' indexing

scala> get"$x[${"a": String}]"
res27: Option[Int] = Some(1)