Francis Toth
Francis Toth

Reputation: 1685

Scala strange behavior "value x is not a member of"

I came upon a strange bug today :

class Klazz[A](a: A) {
  def func[B](f: A => Klazz[B]): B = f(a).a
}

The code seems ok, but the compiler rejects it :

Error:(77, 8) value a not a member of com.sandbox.Klazz[B]
def func[B](f: A => Klazz[B]): B = f(a).a

When I however put this in a case class, there is no problem :

case class Klazz[A](a: A) {
  def a[B](f: A => Klazz[B]): B = f(a).a
}

Any idea why is this happening ?

Thank you in advance.

Upvotes: 2

Views: 264

Answers (1)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149518

case classes automatically set any value in the primary constructor as an immutable field in the class.

With a regular class you need to add the val keyword to the value declaration:

class Klazz[A](val a: A)

Upvotes: 2

Related Questions