Reputation: 1685
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
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