- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Why are class variables required to be initialized?
Sun, 2009-08-02, 22:26
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?
Mon, 2009-08-03, 00:07
#2
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
Mon, 2009-08-03, 01:37
#3
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?
>>
>>
>
>
Mon, 2009-08-03, 01:47
#4
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.
>>
>>
>
>
>
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.
>
>