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

Why are class variables required to be initialized?

4 replies
florin.g
Joined: 2009-07-22,
User offline. Last seen 42 years 45 weeks ago.

If scala is about productivity and conciseness, why wouldn't scala initialize
variables to their default values as it does in many other situations? Sure
you say, in order to infer type yet why not choose to enforce type
declaration val x: Int; rather than val x = 1; when variables are not
initialized?

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Why are class variables required to be initialized?

Why would you declare a variable before the value you want it to hold
is known? Can you give an example?

2009/8/2 florin.g :
>
> If scala is about productivity and conciseness, why wouldn't scala initialize
> variables to their default values as it does in many other situations? Sure
> you say, in order to infer type yet why not choose to enforce type
> declaration val x: Int; rather than val x = 1; when variables are not
> initialized?
>
> --
> View this message in context: http://www.nabble.com/Why-are-class-variables-required-to-be-initialized...
> Sent from the Scala - User mailing list archive at Nabble.com.
>
>

Eastsun 2
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Why are class variables required to be initialized?

Is this what you look for?

scala> var x : Int = _
x: Int = 0

scala> var x : Any = _
x: Any = null

florin.g wrote:
>
> If scala is about productivity and conciseness, why wouldn't scala
> initialize variables to their default values as it does in many other
> situations? Sure you say, in order to infer type yet why not choose to
> enforce type declaration val x: Int; rather than val x = 1; when variables
> are not initialized?
>
>

-----
My scala solutions for http://projecteuler.net/ Project Euler problems:
http://eastsun.javaeye.com/category/34059 Click here

florin.g
Joined: 2009-07-22,
User offline. Last seen 42 years 45 weeks ago.
Re: Why are class variables required to be initialized?

Thanks Eastsun,

I did not mean to be offensive. I've started looking into scala in weekends
and I love its brevity.

Florin

Eastsun wrote:
>
> Is this what you look for?
>
> scala> var x : Int = _
> x: Int = 0
>
> scala> var x : Any = _
> x: Any = null
>
>
> florin.g wrote:
>>
>> If scala is about productivity and conciseness, why wouldn't scala
>> initialize variables to their default values as it does in many other
>> situations? Sure you say, in order to infer type yet why not choose to
>> enforce type declaration val x: Int; rather than val x = 1; when
>> variables are not initialized?
>>
>>
>
>

florin.g
Joined: 2009-07-22,
User offline. Last seen 42 years 45 weeks ago.
Re: Why are class variables required to be initialized?

Ricky,

You're right. I meant 'var' not 'val'.

Thanks.

Ricky Clarkson wrote:
>
> Why would you declare a variable before the value you want it to hold
> is known? Can you give an example?
>
> 2009/8/2 florin.g :
>>
>> If scala is about productivity and conciseness, why wouldn't scala
>> initialize
>> variables to their default values as it does in many other situations?
>> Sure
>> you say, in order to infer type yet why not choose to enforce type
>> declaration val x: Int; rather than val x = 1; when variables are not
>> initialized?
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Why-are-class-variables-required-to-be-initialized...
>> Sent from the Scala - User mailing list archive at Nabble.com.
>>
>>
>
>
>

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