# Implicit resolution quirk

**URL:** <https://users.scala-lang.org/t/implicit-resolution-quirk/528>\
**Category:** Question\
**Created:** [May 27, 2017, 7:07pm UTC](https://users.scala-lang.org/t/implicit-resolution-quirk/528 "2017-05-27T19:07:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![virus\_dave](https://users.scala-lang.org/user_avatar/users.scala-lang.org/virus_dave/32/710_2.png) [@virus\_dave](https://users.scala-lang.org/u/virus_dave)\
**Post date:** [May 27, 2017, 7:07pm UTC](https://users.scala-lang.org/t/implicit-resolution-quirk/528/1 "2017-05-27T19:07:47Z")

</div>

Can anyone explain why the last example here fails to compile?  
[https://scastie.scala-lang.org/AX0uenhmQ6mIQ87xKNeQYg](https://scastie.scala-lang.org/AX0uenhmQ6mIQ87xKNeQYg)

Is this a 2.1x compiler bug?

It compiles under dotty just fine, apparently, but i can’t understand why the implicit referred to in example 2 isn’t found in example 3.

Any insight would be majorly appreciated.

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jasper-m/32/872_2.png) [@Jasper-M](https://users.scala-lang.org/u/Jasper-M)\
**Post date:** [May 27, 2017, 9:11pm UTC](https://users.scala-lang.org/t/implicit-resolution-quirk/528/2 "2017-05-27T21:11:09Z")

</div>

I’m not really sure what the correct behavior here should be. But I think you should generally avoid things like `_ <: FlatShapeLevel` (existentials basically) in implicit parameters. Especially your `tuple2Shape` method looks very fishy to me with its 2 levels of `<:`, `Level <: ShapeLevel` and `_ <: Level`. That looks tough to infer correctly.

I could get it to work by refactoring some `_ <:` stuff: by introducing a type parameter `L <: FlatShapeLevel` in `map` and in `pairItBad`. So even without touching `tuple2Shape` 🙂

Unfortunately I can’t share the working code snippet because for some reason scastie stole my save button…

---

<div class="post-metadata">

**Author:** ![virus\_dave](https://users.scala-lang.org/user_avatar/users.scala-lang.org/virus_dave/32/710_2.png) [@virus\_dave](https://users.scala-lang.org/u/virus_dave)\
**Post date:** [May 27, 2017, 9:33pm UTC](https://users.scala-lang.org/t/implicit-resolution-quirk/528/3 "2017-05-27T21:33:44Z")

</div>

Well this is a start, thanks for checking!

Alas i can’t control this part; all of this code (except the `pair*` functions) correspond to implementation in `Slick`, the Lightbend database project. It’s very much full of stuff like this, so i can’t really change the `Level`s or `map` or the definition of anything other than the `pair*` implementations in the actual source code.

Implicits with existentials are used all over the place (seemingly successfully) throughout slick without problems. I’m curious if anyone else can track down the exact reason for this one not working. I wonder what makes it different from all the other successful ones inside slick?

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jasper-m/32/872_2.png) [@Jasper-M](https://users.scala-lang.org/u/Jasper-M)\
**Post date:** [May 28, 2017, 10:20am UTC](https://users.scala-lang.org/t/implicit-resolution-quirk/528/4 "2017-05-28T10:20:59Z")

</div>

It also appears to work if you only change `pairItBad` like I described in my previous answer. I can’t really say why your version doesn’t work though, especially since it seems to work with dotty. The strangest part is that I just read somewhere that the kind of existentials that require importing scala.language.existentials will be removed in dotty, but they appear to work better in dotty in this piece of code.

---

<div class="post-metadata">

**Author:** ![virus\_dave](https://users.scala-lang.org/user_avatar/users.scala-lang.org/virus_dave/32/710_2.png) [@virus\_dave](https://users.scala-lang.org/u/virus_dave)\
**Post date:** [May 31, 2017, 11:08pm UTC](https://users.scala-lang.org/t/implicit-resolution-quirk/528/5 "2017-05-31T23:08:09Z")

</div>

Any other thoughts on this, anyone? It’s really causing some consternation over here ☹

In the original (non-example) code I tried Jasper’s suggestion for the `pairItBad` case, and even though it works in scastie in reduced example, it does _not_ work in the real code, which when modified in a similar way as suggested has an implementation like:

```auto
object WithRights {
  def forGenericQueryWithIds[AA, BB](
      source: Query[AA, BB, Seq]) = new Object {
    def apply[LEVEL <: FlatShapeLevel](
        getId: (AA) => Rep[Id])(
        implicit queryShape: Shape[LEVEL, AA, BB, AA]) = {
      val mixedSource =
        source
          .joinLeft(otherTable)
          .on(getId(_) === _.Id)

      mixedSource
        .joinLeft(userRights)
        .on { case ((stuff, _), (ur, _)) =>
          ur.entityKey.asColumnOf[Id] === getId(stuff).?
        }
        // This map fails to find the correct Shape for the tuple.
        .map { case ((stuff, _), maybeUR) =>
          (stuff, maybeUR.map(_._2))
        }
    }
  }
}

```

Note the use of intermediate `Object.apply` is to make type inference for `getId` possible at the caller.
