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

Scala Reference and Primary Constructor Parameters

1 reply
Cay Horstmann
Joined: 2009-09-04,
User offline. Last seen 42 years 45 weeks ago.

In section 5.3, the spec waxes eloquent on primary constructor
parameters declared as "var" or "val". But nothing is said about
primary constructor parameters without "var" or "val", except "The
scope of a formal value parameter includes all subsequent
parameter sections and the template t".

Experimentally, it seems that

1) A parameter such as class Foo(x : Int) is turned into a field if it
is referenced in one or more methods
2) Such a field is as if it had been declared as private[this] val x: Int.
3) If the parameter isn't referenced in any method, it does not give
rise to a field.

Is this correct? Is it in the spec?

(I don't think saying that x is in scope implies that x is only object-visible.)

In the Rational example of "Programming in Scala" Section 6.5, it is
written: "Although class parameters n and d are in scope in the code
of your add method, you can only access their value on the object on
which add was invoked." That's consistent with my experiment. It's as
if they had been declared as private[this].

There was a longish discussion that you can access through
http://search.gmane.org/?query=Temporary+Values+in+Constructors+Retained...
(but sadly not through
http://old.nabble.com/Temporary-Values-in-Constructors-Retained-As-Field...)
that seemed to go the other direction, whether a private[this] field
that was only used in the primary constructor could be elided.

This all made me think I am on the right track, but it would be
comforting to know where this is specified. I'd be very grateful if
someone could point me to a definitive account on how a non-var/val
parameter of a primary constructor is handled.

Thanks,

Cay

NB. Example 5.3.1 is less than illuminating in this regard. For
laughs, paste it into the REPL and ask for c.x:

scala> c.x
:8: error: type mismatch;
found : C
required: ?{val x: ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method any2ArrowAssoc in object Predef of type [A](x: A)ArrowAssoc[A]
and method any2Ensuring in object Predef of type [A](x: A)Ensuring[A]
are possible conversion functions from C to ?{val x: ?}
c.x
^

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: Scala Reference and Primary Constructor Parameters
Hi Kay.

On Mon, Dec 13, 2010 at 3:49 PM, Cay Horstmann <cay [dot] horstmann [at] gmail [dot] com> wrote:
In section 5.3, the spec waxes eloquent on primary constructor
parameters declared as "var" or "val". But nothing is said about
primary constructor parameters without "var" or "val", except "The
scope of a formal value parameter includes all subsequent
parameter sections and the template t".

Experimentally, it seems that

1) A parameter such as class Foo(x : Int) is turned into a field if it
is referenced in one or more methods
2) Such a field is as if it had been declared as private[this] val x: Int.
3) If the parameter isn't referenced in any method, it does not give
rise to a field.

 That's all true, but it should be treated as an implementation technique. That's why the spec is silent about it.

Cheers

 -- Martin

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