Conflict with named constructor arguments

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Conflict with named constructor arguments

Hanns Holger Rutz
hi,

is there any possibility to get this working:

class Test( test: Int = 99 ) {
        private var testVar = test
        def test = testVar
}

so that testVar is publically read-only, but i can change its update its value internally, and i don't need to screw the constructor's argument name because i want to be able to use it with named argument. so this:

class Test( private var testVar: Int = 99 ) {
        def test = testVar
}

is not acceptable, as i would need to construct new Test( testVar = 33 ), although i want new Test( test = 33 ). to have properly isolated constructor methods was one of the advantages of java....

thanks, -sciss-

Reply | Threaded
Open this post in threaded view
|

Re: Conflict with named constructor arguments

Kevin Wright-4
As always, stack overflow is your friend:


The good questions do keep coming up time and time again :)




On 9 May 2010 16:25, Sciss <[hidden email]> wrote:
hi,

is there any possibility to get this working:

class Test( test: Int = 99 ) {
       private var testVar = test
       def test = testVar
}

so that testVar is publically read-only, but i can change its update its value internally, and i don't need to screw the constructor's argument name because i want to be able to use it with named argument. so this:

class Test( private var testVar: Int = 99 ) {
       def test = testVar
}

is not acceptable, as i would need to construct new Test( testVar = 33 ), although i want new Test( test = 33 ). to have properly isolated constructor methods was one of the advantages of java....

thanks, -sciss-




--
Kevin Wright

mail/google talk: [hidden email]
wave: [hidden email]
skype: kev.lee.wright
twitter: @thecoda

Reply | Threaded
Open this post in threaded view
|

Re: Conflict with named constructor arguments

Hanns Holger Rutz
thanks,

i worked around it now by now passing in stateful variables in the constructor. anyway cleaner.....

best, -sciss-

Am 09.05.2010 um 20:46 schrieb Kevin Wright:

> As always, stack overflow is your friend:
>
> http://stackoverflow.com/questions/2023077/how-to-expose-a-constructor-variablesic-as-read-only/
>
> The good questions do keep coming up time and time again :)
>
>
>
>
> On 9 May 2010 16:25, Sciss <[hidden email]> wrote:
> hi,
>
> is there any possibility to get this working:
>
> class Test( test: Int = 99 ) {
>        private var testVar = test
>        def test = testVar
> }
>
> so that testVar is publically read-only, but i can change its update its value internally, and i don't need to screw the constructor's argument name because i want to be able to use it with named argument. so this:
>
> class Test( private var testVar: Int = 99 ) {
>        def test = testVar
> }
>
> is not acceptable, as i would need to construct new Test( testVar = 33 ), although i want new Test( test = 33 ). to have properly isolated constructor methods was one of the advantages of java....
>
> thanks, -sciss-
>
>
>
>
> --
> Kevin Wright
>
> mail/google talk: [hidden email]
> wave: [hidden email]
> skype: kev.lee.wright
> twitter: @thecoda
>

Reply | Threaded
Open this post in threaded view
|

Re: Conflict with named constructor arguments

Hanns Holger Rutz
should read: by _not_ passing

Am 09.05.2010 um 20:51 schrieb Sciss:

> thanks,
>
> i worked around it now by now passing in stateful variables in the constructor. anyway cleaner.....
>
> best, -sciss-
>
> Am 09.05.2010 um 20:46 schrieb Kevin Wright:
>
>> As always, stack overflow is your friend:
>>
>> http://stackoverflow.com/questions/2023077/how-to-expose-a-constructor-variablesic-as-read-only/
>>
>> The good questions do keep coming up time and time again :)
>>
>>
>>
>>
>> On 9 May 2010 16:25, Sciss <[hidden email]> wrote:
>> hi,
>>
>> is there any possibility to get this working:
>>
>> class Test( test: Int = 99 ) {
>>       private var testVar = test
>>       def test = testVar
>> }
>>
>> so that testVar is publically read-only, but i can change its update its value internally, and i don't need to screw the constructor's argument name because i want to be able to use it with named argument. so this:
>>
>> class Test( private var testVar: Int = 99 ) {
>>       def test = testVar
>> }
>>
>> is not acceptable, as i would need to construct new Test( testVar = 33 ), although i want new Test( test = 33 ). to have properly isolated constructor methods was one of the advantages of java....
>>
>> thanks, -sciss-
>>
>>
>>
>>
>> --
>> Kevin Wright
>>
>> mail/google talk: [hidden email]
>> wave: [hidden email]
>> skype: kev.lee.wright
>> twitter: @thecoda
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: Conflict with named constructor arguments

Eric Willigers
In reply to this post by Hanns Holger Rutz
Sciss wrote:
> thanks,
>
> i worked around it now by now.....


Here are two more workarounds:-


// Approach 1 - use inheritance

class Test1Base protected ( _test: Int ) {
        private var testVar: Int = _test
        def test = testVar
}

class Test1( test: Int = 99 ) extends Test1Base(test)

object ExerciseTest1
{
     def main(args: Array[String]) {
         println(new Test1( test = 33 ).test)
         println(new Test1().test)
     }
}

// Approach 2 - use companion object apply

class Test2 private ( _test: Int ) {
        private var testVar: Int = _test
        def test = testVar
}

object Test2 {
     def apply( test: Int = 99 ) = new Test2(test)
}

object ExerciseTest2
{
     def main(args: Array[String]) {
         println(Test2( test = 33 ).test)
         println(Test2().test)
     }
}

Reply | Threaded
Open this post in threaded view
|

Re: Conflict with named constructor arguments

Hanns Holger Rutz
good ideas! i like mostly the second one....

Am 10.05.2010 um 01:05 schrieb Eric Willigers:

> Sciss wrote:
>> thanks,
>> i worked around it now by now.....
>
>
> Here are two more workarounds:-
>
>
> // Approach 1 - use inheritance
>
> class Test1Base protected ( _test: Int ) {
> private var testVar: Int = _test
> def test = testVar
> }
>
> class Test1( test: Int = 99 ) extends Test1Base(test)
>
> object ExerciseTest1
> {
>    def main(args: Array[String]) {
>        println(new Test1( test = 33 ).test)
>        println(new Test1().test)
>    }
> }
>
> // Approach 2 - use companion object apply
>
> class Test2 private ( _test: Int ) {
> private var testVar: Int = _test
> def test = testVar
> }
>
> object Test2 {
>    def apply( test: Int = 99 ) = new Test2(test)
> }
>
> object ExerciseTest2
> {
>    def main(args: Array[String]) {
>        println(Test2( test = 33 ).test)
>        println(Test2().test)
>    }
> }
>