|
hello list,
Is there a super() method in Scala that I can do things like this? class A(x:Int){ override def toString={x.toString} } class B extends A(0) { def this(s:String)={ super(s.toInt) //<-- error: 'this' expected but 'super' found. } } Thanks, Zemian Deng |
|
On 2008-02-21 13:29:19 Zemian Deng wrote:
> hello list, > > Is there a super() method in Scala that I can do things like this? > > class A(x:Int){ > override def toString={x.toString} > } > class B extends A(0) { > def this(s:String)={ > super(s.toInt) //<-- error: 'this' expected but 'super' found. > } > } No. Superclass parameters can only be specified in the primary constructor. This is a limitation of Scala compared to Java, but it's really just enforcing good practise. In your example, you could do: class B private (x : Int) extends A(x) { def this = this(0) def this(s : String) = this(s.toInt) } /J |
|
In reply to this post by Zemian Deng
Zemian Deng wrote:
> hello list, > > Is there a super() method in Scala that I can do things like this? > > class A(x:Int){ > override def toString={x.toString} > } > class B extends A(0) { > def this(s:String)={ > super(s.toInt) //<-- error: 'this' expected but 'super' found. No, there isn't a way. You have to use the primary constructor. Blair |
|
In reply to this post by Zemian Deng
Zemian,
You can don't need the extra constructor, you can to the toInt conversion inline class B(s:String) extends A(s.toInt) { } Steve On Thu, Feb 21, 2008 at 12:29 PM, Zemian Deng <[hidden email]> wrote: hello list, |
|
In reply to this post by Zemian Deng
For the given example, this should do:
class B(s: String) extends A(s.toInt) On Thu, Feb 21, 2008 at 8:29 PM, Zemian Deng <[hidden email]> wrote: > hello list, > > Is there a super() method in Scala that I can do things like this? > > class A(x:Int){ > override def toString={x.toString} > } > class B extends A(0) { > def this(s:String)={ > super(s.toInt) //<-- error: 'this' expected but 'super' found. > } > } > > Thanks, > Zemian Deng > -- __~O -\ <, Christos KK Loverdos (*)/ (*) http://ckkloverdos.com |
|
In reply to this post by Zemian Deng
Zemian Deng wrote:
> hello list, > > Is there a super() method in Scala that I can do things like this? > > class A(x:Int){ > override def toString={x.toString} > } > class B extends A(0) { > def this(s:String)={ > super(s.toInt) //<-- error: 'this' expected but 'super' found. > } > } > > Thanks, > Zemian Deng No. You'll need class B private(n: Int) extends A(n) { def this(s:String)={ this(s.toInt) } def this()={ this(0) } } The "private" is optional - it allows you to still only expose the same two constructors to users. |
|
Hum... maybe the example I provided is not very good. So let's try again. If I have a closed source Java library that provided this:
class S1 class S2 class A -> has 3 over loaded constructors that I can do following: new A() new A(new S1) new A(new S2) And unfortunately the class A is closed source and no setter methods is exposed! S1 and S2 is not related and not convertible. Now if I were to write a Scala class that inherit from A and want to cover all constructor cases, my first thought was I can do things with a super constructor call like ... class B extends A{ def this(s1:S1) super(s1) def this(s2:S2) super(s2) } But now that I know super doesn't exists, but I still can't find a way to cover all constructors. Can you provide me a sample of workaround again based on above scenerio? Much thanks, -zemian On Thu, Feb 21, 2008 at 5:15 PM, Eric Willigers <[hidden email]> wrote:
|
|
It seems that you could do this:
class B extends A class BWithS1(s1: S1) extends A(s1) class BWithS2(s2: S2) extends A(s2) I don't think it's possible without creating a separate type in Scala for each constructor. Zemian Deng wrote: > Hum... maybe the example I provided is not very good. So let's try > again. If I have a closed source Java library that provided this: > class S1 > class S2 > class A -> has 3 over loaded constructors that I can do following: > new A() > new A(new S1) > new A(new S2) > And unfortunately the class A is closed source and no setter methods > is exposed! S1 and S2 is not related and not convertible. > > Now if I were to write a Scala class that inherit from A and want to > cover all constructor cases, my first thought was I can do things with > a super constructor call like ... > class B extends A{ > def this(s1:S1) super(s1) > def this(s2:S2) super(s2) > } > > But now that I know super doesn't exists, but I still can't find a way > to cover all constructors. Can you provide me a sample of workaround > again based on above scenerio? > > Much thanks, > -zemian > > > On Thu, Feb 21, 2008 at 5:15 PM, Eric Willigers <[hidden email] > <mailto:[hidden email]>> wrote: > > Zemian Deng wrote: > > hello list, > > > > Is there a super() method in Scala that I can do things like this? > > > > class A(x:Int){ > > override def toString={x.toString} > > } > > class B extends A(0) { > > def this(s:String)={ > > super(s.toInt) //<-- error: 'this' expected but 'super' found. > > } > > } > > > > Thanks, > > Zemian Deng > > No. You'll need > > class B private(n: Int) extends A(n) { > def this(s:String)={ > this(s.toInt) > } > > def this()={ > this(0) > } > } > > The "private" is optional - it allows you to still only expose the > same > two constructors to users. > > |
| Powered by Nabble | Edit this page |
