Finkelson
Finkelson

Reputation: 3013

How can I override a function of type member in Scala?

Suppose I have a code:

class C extends A#B {
  override def fun(): Int = 100
}

trait A {
  type B = { 
    def fun(): Int 
  }
}

The compiler says:

class type required but Object{def fun(): Int} found
  class C extends A#B {
                    ^

How can I understand that error?

Upvotes: 1

Views: 29

Answers (1)

Till Rohrmann
Till Rohrmann

Reputation: 13346

You cannot extend a structural type in Scala. Structural types only denote a bunch of methods which a type has to define in order to be used at places where the structural type is expected.

Thus, it is sufficient to write

class C {
  def fun(): Int = 100
}

to pass objects of type C to variables of B.

Upvotes: 3

Related Questions