Using context bounds with more than one type parameter (=:=)

I have the following function:

  def combine1[L1 <: S, L2 <: S, T1, T2](a: BaseSampler[L1,T1], b: BaseSampler[L2,T2])(implicit ev1: L1 =:= L2 ) =
    ComplexSampler[L1, T1, T2](a.x, b.x)

that works correctly. However the Scala compiler (2.12.2) complaints that the ev is not used. So I thought I could use the implicitly construct and tried this:

  def combine1[L1 <: S, L2 <: S, T1, T2](a: BaseSampler[L1,T1], b: BaseSampler[L2,T2]) = {
    implicitly[L1 =:= L2]
    ComplexSampler[L1, T1, T2](a.x, b.x)

which fails with:

Cannot prove that L1 =:= L2.
[error]     implicitly[L1 =:= L2]
[error]               ^

Of course this fails because I have no context bound for =:= for two type parameters. The question now is, how can I add that context bound? I have found a possible solution in [1] but cannot get it to work.

Can anyone point me in the right direction?



It looks like you don’t really need the L2 type parameter to the combine1 method at all. Can you try leaving it out? E.g.:

def combine1[L <: S, T1, T2](a: BaseSampler[L, T1], b: BaseSampler[L, T2]) =
  ComplexSampler[L, T1, T2](a.x, b.x)

The issue is that if you have L1 <: S and also L2 <: S, you can’t at the same time say that L1 =:= L2. You have to get rid of one or the other assertion.

Oops. Yeah, that is a trivial solution to a bad example. What I really want is to do things like implicitly[L1 =:= S1] were S1 extends S. I would actually have two or more implicit parameters.