0

I have this:

final case class MyClass(....) extends G with M[MyClass]

object MyObject extends GMC[MyClass] {....}

trait GMC[A <: G with M[A]] {....}

def f(gmc: GMC[G])

f(MyObject)

I get compiler error as:

Type mismatch: Required GMC[G], found MyObject.type

Is this wrong way of passing singleton?

3
  • Does X conforms to T[A] what is A? Which type did X used when inheriting T? Is T covariant, contravariant or invariant? Commented Mar 16, 2020 at 13:06
  • 2
    Please show us a complete, self-contained example, ie. the exact same code you use which reproduces the compiler error. Commented Mar 16, 2020 at 13:07
  • Where is G where is M? f doesn't compile- Commented Mar 16, 2020 at 13:35

1 Answer 1

2

Answer to updated question

The problem lies with the definition of f. If you remove the call of f you get this error message:

type arguments [G] do not conform to trait GMC's type parameter bounds [A <: G with M[A]]

In other words, you can't use a bare G as a type parameter for GMC where the type constraint for GMC is G with M[A].

You need to constrain the type that f takes to match the definition of GMC:

def f[A <: G with M[A]](gmc: GMC[A]): Unit = ???

Previous answer

You need to specify the type parameter for T when you create the object:

object X extends T[A] {....}

If you make that change it will compile:

class A
trait T[A]
object X extends T[A] {}
def f(t: T[A]) = {}

f(X) // No error
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.