-1

I was playing around with dependent types and run into this error:

some.scala:17: error: class B needs to be abstract, since value element in trait
 Buffer of type B.this.T is not defined

using this code:

package types

object Demo {

trait Buffer {
  type T
  val element: T
}

abstract class Wee extends Buffer {
    type T <:    Integer
    val s = element - 2

}

class B extends Wee {
}

}

Could someone decrypt what value element stands for?

1 Answers1

2

The first problem is that you're using an abstract type member for no conceivable reason. It's not directly related to your error, but alas.

package types

object Demo {

trait Buffer[T] {
  def element: T
}

abstract class Wee extends Buffer[Integer] {
  def s: Integer = element - 2

}

Ok now for the actual failure, it's pretty obvious and straightforward, you have not provided an implementation for element. Your Wee class extends Buffer, so any concrete implementor must provide an implementation for element by your very contract.

A trait or abstract class can have abstract members, a normal class cannot. Ergo, kaboom, compiler is telling you to get your OOP up to scratch.

class B extends Wee {
  val element = 5
}

Or better yet:

class B(val element: Integer) extends Wee
flavian
  • 28,161
  • 11
  • 65
  • 105