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

Constructor parameters

2 replies
j.romildo
Joined: 2009-01-04,
User offline. Last seen 42 years 45 weeks ago.

Hello.

I am new to Scala, although I have some experience with functional
programming and with Java.

There is something I do not understand yet regarding constructors in
Scala.

Consider the following application, which is accepted by the Scala
compiler:

class Test(xs: List[String]) {
val v: Array[String] = xs.toArray
def test1 = v.foreach(println)
def test2 = println(this.xs)
}

object MakeTest {
def main(args: Array[String]) {
val t = new Test(List("a", "b", "c"))
t.test1
t.test2
}
}

Is xs a member of class Test?

The keyword this refers to the object instance on which the currently
executing method was invoked. So the notation this.xs suggests that xs
is a member of the object. If it is not a member, then what does this.xs
mean?

What is the lifetime of xs? I suppose it is the whole lifetime of the
object, right?

Suppose the method test2 is removed from the class. In this case, what
is the lifetime of xs? Would it still be the lifetime of the object? In
this case I think it should be considered garbage as soon as the
activation of the construcor finishes. Is it the case?

Please, comment on this issues so that I can better understand Scala.

Regards,

José Romildo

Steve Bendiola
Joined: 2008-12-27,
User offline. Last seen 42 years 45 weeks ago.
Re: Constructor parameters

All the variables in the constructor becomes instance variables

On Sun, Jan 4, 2009 at 8:06 AM, wrote:
> Hello.
>
> I am new to Scala, although I have some experience with functional
> programming and with Java.
>
> There is something I do not understand yet regarding constructors in
> Scala.
>
> Consider the following application, which is accepted by the Scala
> compiler:
>
> class Test(xs: List[String]) {
> val v: Array[String] = xs.toArray
> def test1 = v.foreach(println)
> def test2 = println(this.xs)
> }
>
> object MakeTest {
> def main(args: Array[String]) {
> val t = new Test(List("a", "b", "c"))
> t.test1
> t.test2
> }
> }
>
> Is xs a member of class Test?
>
> The keyword this refers to the object instance on which the currently
> executing method was invoked. So the notation this.xs suggests that xs
> is a member of the object. If it is not a member, then what does this.xs
> mean?
>
> What is the lifetime of xs? I suppose it is the whole lifetime of the
> object, right?
>
> Suppose the method test2 is removed from the class. In this case, what
> is the lifetime of xs? Would it still be the lifetime of the object? In
> this case I think it should be considered garbage as soon as the
> activation of the construcor finishes. Is it the case?
>
> Please, comment on this issues so that I can better understand Scala.
>
> Regards,
>
> José Romildo
>
>

ewilligers
Joined: 2008-08-20,
User offline. Last seen 3 years 17 weeks ago.
Re: Constructor parameters

j [dot] romildo [at] gmail [dot] com wrote:
> There is something I do not understand yet regarding constructors in
> Scala.

If the class is a case class, or the constructor parameter is declared
with a val or a var, the constructor parameter becomes a member.
Otherwise, if the constructor parameter is needed after construction
(for example by a member method or a member lazy value), it is compiled
into a private member.
Otherwise, it does not become a member.

> Consider the following application, which is accepted by the Scala
> compiler:
>
> class Test(xs: List[String]) {
> val v: Array[String] = xs.toArray
> def test1 = v.foreach(println)
> def test2 = println(this.xs)
> }
>
> object MakeTest {
> def main(args: Array[String]) {
> val t = new Test(List("a", "b", "c"))
> t.test1
> t.test2
> }
> }
>
> Is xs a member of class Test?

> The keyword this refers to the object instance on which the currently
> executing method was invoked. So the notation this.xs suggests that xs
> is a member of the object. If it is not a member, then what does this.xs
> mean?

Not an official member in the Scala language spec sense, but in the
generated bytecode it would be a private member.

> What is the lifetime of xs? I suppose it is the whole lifetime of the
> object, right?

Yes

> Suppose the method test2 is removed from the class. In this case, what
> is the lifetime of xs? Would it still be the lifetime of the object? In
> this case I think it should be considered garbage as soon as the
> activation of the construcor finishes. Is it the case?

In that case, the lifetime ends when the constructor finishes.

Here's some testing code you might find useful. For me it prints
Finalized
Done

Change C to be a case class, or have a member method using a, and then
Done
will be the first (and possibly only) thing printed.

class F {
override def finalize() {
println("Finalized")
}
}

class C(a: F) {
}

object Main extends Application {
def makeC() = new C(new F())

val c = makeC()
System.gc()
System.runFinalization()
println("Done")
}

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