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

Why do I need to initialize every member

3 replies
Mark Kerzner
Joined: 2011-05-04,
User offline. Last seen 42 years 45 weeks ago.
For example, 

class Persist extends Logging {  var connector: Connect = new Connect  var client: Cassandra.Client = connector.connect
I would expect to be able to say
  var connector: Connect
but this does not compile. At least can I set it to some kind of null?
Thank you,Mark
Ken McDonald
Joined: 2011-02-13,
User offline. Last seen 42 years 45 weeks ago.
Re: Why do I need to initialize every member
say
  var connector: Connect = _
when you simply say
  var connector: Connect
your are declaring "connector" as an abstract var.
Ken
Russ P.
Joined: 2009-01-31,
User offline. Last seen 1 year 26 weeks ago.
Re: Re: Why do I need to initialize every member
On Mon, May 23, 2011 at 12:56 PM, Ken McDonald <ykkenmcd@gmail.com> wrote:
say
  var connector: Connect = _
when you simply say
  var connector: Connect
your are declaring "connector" as an abstract var.
Ken


Interesting. I was not aware of that feature. What exactly does it do?

--Russ P

--
http://RussP.us
Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: Re: Why do I need to initialize every member

On Mon, May 23, 2011 at 11:03 PM, Russ Paielli wrote:
> On Mon, May 23, 2011 at 12:56 PM, Ken McDonald wrote:
>>   var connector: Connect = _

> Interesting. I was not aware of that feature. What exactly does it do?

See Section 4.2 "Variable Declarations and Definitions" (p 40) of the spec. [1]

"A variable definition var x: T = _ can appear only as a member of a
template. It introduces a mutable field with type T and a default
initial value. The default value depends on the type T as follows:
[... 0 / false / () / null]

-jason

[1] http://www.scala-lang.org/docu/files/ScalaReference.pdf

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