- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
why override case class parameters?
Wed, 2009-03-04, 15:03
He,
I still don't understand why one has to declare class parameters of case classes to be ``overriden''. For example:
abstract class A(id: String)
case class B(id: String, f: File) extends A(id)
case class C(override id: String, override f: File, r: String) extends B(id, f)
If, however, I declare case class C like
case class C(_id: String, _f: File, r: String) extends B(_id, _f)
I don't have to use ``override''.
The thing is, case class C is just a simple extension of case class B by adding an additional value ``r''.
In addition, if I want to extend case class C I have the same problem and could do the same trick:
case class D(__id: String, __f: File, _r: String, s: Int) extends C(__id, __f, __r)
Can anyone explain, what am I doing here, and why I have to do this?
Cheers,
Wed, 2009-03-04, 15:47
#2
Re: why override case class parameters?
Paul Phillips wrote:
> On Wed, Mar 04, 2009 at 03:03:30PM +0100, Normen Müller wrote:
>> abstract class A(id: String)
>> case class B(id: String, f: File) extends A(id)
>> case class C(override id: String, override f: File, r: String) extends B(id, f)
>
> Case classes extending case classes is a real can of worms. There are
> many issues which have not been resolved and if they are going to
> continue to be supported they need to be respecified to deal with issues
> like this one.
That would be nice!
> The reason it happens is that case classes automatically make vals out
> of all their constructor parameters, and you can't declare a val in a
> subclass with the same name as one in a superclass without overriding.
> You could also have class B declare its parameters private, but this
> would be very dicey because it's probably not what you want.
I thought of sth. like this. Thanks for the explanation. So I'll use ``override'' even it messes up my code wrt. compactness/ readability ;)
Cheers,
Wed, 2009-03-04, 15:57
#3
Re: why override case class parameters?
Paul Phillips wrote:
> On Wed, Mar 04, 2009 at 03:03:30PM +0100, Normen Müller wrote:
>> abstract class A(id: String)
>> case class B(id: String, f: File) extends A(id)
>> case class C(override id: String, override f: File, r: String) extends B(id, f)
BTW, do you know why
case class C(_id, _f, r) extends B(_id, f)
works w/o ``override''? Is this an undocumented feature, or what is happening here?
Cheers,
Wed, 2009-03-04, 15:57
#4
Re: why override case class parameters?
On Wed, Mar 04, 2009 at 03:36:05PM +0100, Normen Müller wrote:
> BTW, do you know why
>
> case class C(_id, _f, r) extends B(_id, f)
>
> works w/o ``override''? Is this an undocumented feature, or what is
> happening here?
Because now you're not overriding anything.
case class B(id: String, f: File)
That declares vals id and f.
case class C(_id: String, _f: File, r: String) extends B(_id, _f)
That declares vals _id, _f, and r. Now you have five vals. They're all
in there, see:
scala> C("bob", null, null)
res0: C = C(bob,null,null)
scala> res0._id
res1: String = bob
scala> res0.id
res2: String = bob
Wed, 2009-03-04, 16:07
#5
Re: why override case class parameters?
Normen Müller schrieb:
> BTW, do you know why
>
> case class C(_id, _f, r) extends B(_id, f)
>
> works w/o ``override''?
You're just adding more fields. Try and define:
abstract class A(id: String)
case class B(id: String, f: File) extends A(id)
case class C(_id: String, _f: String, r: String) extends B(_id, _f)
And then:
scala> new C("Foo", "Bar", "Baz")
res0: C = C(Foo,Bar,Baz)
scala> res0.id
res1: String = Foo
scala> res0._id
res2: String = Foo
- Florian.
Wed, 2009-03-04, 17:37
#6
Re: why override case class parameters?
(When you write extends B(_id, _f) it's the equivalent of the following in Java:
public C(String _id, File _f, String _r) {
super(_id, _f, _r);
this._id = _id;
this._f = _f;
this._r = _r;
}
)
On Wed, Mar 4, 2009 at 9:47 AM, Paul Phillips <paulp@improving.org> wrote:
public C(String _id, File _f, String _r) {
super(_id, _f, _r);
this._id = _id;
this._f = _f;
this._r = _r;
}
)
On Wed, Mar 4, 2009 at 9:47 AM, Paul Phillips <paulp@improving.org> wrote:
On Wed, Mar 04, 2009 at 03:36:05PM +0100, Normen Müller wrote:
> BTW, do you know why
>
> case class C(_id, _f, r) extends B(_id, f)
>
> works w/o ``override''? Is this an undocumented feature, or what is
> happening here?
Because now you're not overriding anything.
case class B(id: String, f: File)
That declares vals id and f.
case class C(_id: String, _f: File, r: String) extends B(_id, _f)
That declares vals _id, _f, and r. Now you have five vals. They're all
in there, see:
scala> C("bob", null, null)
res0: C = C(bob,null,null)
scala> res0._id
res1: String = bob
scala> res0.id
res2: String = bob
--
Paul Phillips | Simplicity and elegance are unpopular because
Imperfectionist | they require hard work and discipline to achieve
Empiricist | and education to be appreciated.
pal, i pill push | -- Dijkstra
Wed, 2009-03-04, 17:47
#7
Re: why override case class parameters?
(More specifically the first line)
On Wed, Mar 4, 2009 at 11:23 AM, Naftoli Gugenheim <naftoligug@gmail.com> wrote:
On Wed, Mar 4, 2009 at 11:23 AM, Naftoli Gugenheim <naftoligug@gmail.com> wrote:
(When you write extends B(_id, _f) it's the equivalent of the following in Java:
public C(String _id, File _f, String _r) {
super(_id, _f, _r);
this._id = _id;
this._f = _f;
this._r = _r;
}
)
On Wed, Mar 4, 2009 at 9:47 AM, Paul Phillips <paulp@improving.org> wrote:
On Wed, Mar 04, 2009 at 03:36:05PM +0100, Normen Müller wrote:
> BTW, do you know why
>
> case class C(_id, _f, r) extends B(_id, f)
>
> works w/o ``override''? Is this an undocumented feature, or what is
> happening here?
Because now you're not overriding anything.
case class B(id: String, f: File)
That declares vals id and f.
case class C(_id: String, _f: File, r: String) extends B(_id, _f)
That declares vals _id, _f, and r. Now you have five vals. They're all
in there, see:
scala> C("bob", null, null)
res0: C = C(bob,null,null)
scala> res0._id
res1: String = bob
scala> res0.id
res2: String = bob
--
Paul Phillips | Simplicity and elegance are unpopular because
Imperfectionist | they require hard work and discipline to achieve
Empiricist | and education to be appreciated.
pal, i pill push | -- Dijkstra
On Wed, Mar 04, 2009 at 03:03:30PM +0100, Normen Müller wrote:
> abstract class A(id: String)
> case class B(id: String, f: File) extends A(id)
> case class C(override id: String, override f: File, r: String) extends B(id, f)
Case classes extending case classes is a real can of worms. There are
many issues which have not been resolved and if they are going to
continue to be supported they need to be respecified to deal with issues
like this one.
The reason it happens is that case classes automatically make vals out
of all their constructor parameters, and you can't declare a val in a
subclass with the same name as one in a superclass without overriding.
You could also have class B declare its parameters private, but this
would be very dicey because it's probably not what you want.