This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Overriding a method that returns a repeated parameter

2 replies
paulbutcher
Joined: 2010-03-08,
User offline. Last seen 10 weeks 5 days ago.

Given a method that returns a repeated parameter (getXs below):

> scala> class A(xs: Int*) { def getXs = xs }
> defined class A

Is there any way to override that method? I've tried a whole pile of different constructs, none of which work:

> scala> class B extends A { override def getXs: Seq[Int] = Seq() }
> :8: error: overriding method getXs in class A of type => Int*;
> method getXs has incompatible type
> class B extends A { override def getXs: Seq[Int] = Seq() }
> ^
>
> scala> class B extends A { override def getXs: collection.mutable.WrappedArray[Int] = Array[Int]() }
> :8: error: overriding method getXs in class A of type => Int*;
> method getXs has incompatible type
> class B extends A { override def getXs: collection.mutable.WrappedArray[Int] = Array[Int]() }
> ^
>
> scala> class B extends A { override def getXs: Int* = Array[Int]() }
> :1: error: '=' expected but identifier found.
> class B extends A { override def getXs: Int* = Array[Int]() }
> ^
> :1: error: illegal start of simple expression
> class B extends A { override def getXs: Int* = Array[Int]() }
> ^

Is there any way to do this? I did find this related question on SO, but the answer was (to me, at least) unedifying:

http://stackoverflow.com/questions/5512402/overriding-a-repeated-class-p...

--
paul.butcher->msgCount++

Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?

http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: paul@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: Overriding a method that returns a repeated parameter
On Fri, Jan 20, 2012 at 7:31 PM, Paul Butcher <paul@paulbutcher.com> wrote:
Given a method that returns a repeated parameter (getXs below):

> scala> class A(xs: Int*) { def getXs = xs }
> defined class A

Is there any way to override that method? I've tried a whole pile of different constructs, none of which work:

> scala> class B extends A { override def getXs: Seq[Int] = Seq() }
> <console>:8: error: overriding method getXs in class A of type => Int*;
>  method getXs has incompatible type
>        class B extends A { override def getXs: Seq[Int] = Seq() }
>                                         ^
>
> scala> class B extends A { override def getXs: collection.mutable.WrappedArray[Int] = Array[Int]() }
> <console>:8: error: overriding method getXs in class A of type => Int*;
>  method getXs has incompatible type
>        class B extends A { override def getXs: collection.mutable.WrappedArray[Int] = Array[Int]() }
>                                         ^
>
> scala> class B extends A { override def getXs: Int* = Array[Int]() }
> <console>:1: error: '=' expected but identifier found.
>        class B extends A { override def getXs: Int* = Array[Int]() }
>                                                   ^
> <console>:1: error: illegal start of simple expression
>        class B extends A { override def getXs: Int* = Array[Int]() }
>                                                                    ^


Is there any way to do this? I did find this related question on SO, but the answer was (to me, at least) unedifying:

http://stackoverflow.com/questions/5512402/overriding-a-repeated-class-parameter-in-scala

See: https://issues.scala-lang.org/browse/SI-4176
-jason 
paulbutcher
Joined: 2010-03-08,
User offline. Last seen 10 weeks 5 days ago.
Re: Overriding a method that returns a repeated parameter
On 20 Jan 2012, at 18:39, Jason Zaugg wrote:
See: https://issues.scala-lang.org/browse/SI-4176

Great - thanks Jason!
--
paul.butcher->msgCount++

Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?

http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: paul@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland