initialization of val fields

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

initialization of val fields

Szabolcs Berecz
Hi,

I have this ugly code:

class C(headLen_ : Int, functions: Alphabet[Func]) {
  protected val (headLen, tailLen) = {
      if (functions.length == 0)
        (0, headLen_ * (functions.maxParams - 1) + 1 + headLen_)
      else
        (headLen_, headLen_ * (functions.maxParams - 1) + 1)
    }
}

Can you recommend me a better way to do it? The only better way I
could come up with is to put the initialization in the companion
object az a protected method so that I could write this:

class C(headLen_ : Int, functions: Alphabet[Func]) {
  protected val (headLen, tailLen) = headTailInit(headLen_, functions)
}

It's much better but I still don't like it. I'm used to initializing
everything in the constructor, so that it's clearly separated. I don't
know how I could do the same in Scala.

Also, I need to call a public method as part of the construction, so
in reality it would look more like:

class C(headLen_ : Int, functions: Alphabet[Func]) {
  protected val (headLen, tailLen) = headTailInit(headLen_, functions)
  randomize()
}

Add a bunch of other methods and it's a complete mess.

BR/Szabi

--
DRM: Digital Restrictions Management -- learn about the dangers at
http://www.defectivebydesign.org/what_is_drm
Reply | Threaded
Open this post in threaded view
|

Re: initialization of val fields

Jan Lohre
You could change C's constructor to take headLen and tailLen as parameters and create an apply method in a companion object to do the calculations.

class C(val headLen : Int, val tailLen : Int)

object C {
  def apply(headLen_ : Int, functions: Alphabet[Func]) : C = {
    if (functions.length == 0)
       new C(0, headLen_ * (functions.maxParams - 1) + 1 + headLen_)
     else
       new C(headLen_, headLen_ * (functions.maxParams - 1) + 1)
  }
}

In client code you can do:

def test() {
  ...
  val c = C(32, myFunctions)
  ...
}

Note that I currently have no scala environment at hand so I couldn't compile let alone test the code above.

Kind regards,
Jan

2009/4/17 Szabolcs Berecz <[hidden email]>
Hi,

I have this ugly code:

class C(headLen_ : Int, functions: Alphabet[Func]) {
 protected val (headLen, tailLen) = {
     if (functions.length == 0)
       (0, headLen_ * (functions.maxParams - 1) + 1 + headLen_)
     else
       (headLen_, headLen_ * (functions.maxParams - 1) + 1)
   }
}

Can you recommend me a better way to do it? The only better way I
could come up with is to put the initialization in the companion
object az a protected method so that I could write this:

class C(headLen_ : Int, functions: Alphabet[Func]) {
 protected val (headLen, tailLen) = headTailInit(headLen_, functions)
}

It's much better but I still don't like it. I'm used to initializing
everything in the constructor, so that it's clearly separated. I don't
know how I could do the same in Scala.

Also, I need to call a public method as part of the construction, so
in reality it would look more like:

class C(headLen_ : Int, functions: Alphabet[Func]) {
 protected val (headLen, tailLen) = headTailInit(headLen_, functions)
 randomize()
}

Add a bunch of other methods and it's a complete mess.

BR/Szabi

--
DRM: Digital Restrictions Management -- learn about the dangers at
http://www.defectivebydesign.org/what_is_drm

Reply | Threaded
Open this post in threaded view
|

Re: initialization of val fields

Szabolcs Berecz
On Fri, Apr 17, 2009 at 13:04, Jan Lohre <[hidden email]> wrote:

> You could change C's constructor to take headLen and tailLen as parameters
> and create an apply method in a companion object to do the calculations.
>
> class C(val headLen : Int, val tailLen : Int)
>
> object C {
>   def apply(headLen_ : Int, functions: Alphabet[Func]) : C = {
>     if (functions.length == 0)
>        new C(0, headLen_ * (functions.maxParams - 1) + 1 + headLen_)
>      else
>        new C(headLen_, headLen_ * (functions.maxParams - 1) + 1)
>   }
> }

I like it! Thanks!

>
> Kind regards,
> Jan
>
> 2009/4/17 Szabolcs Berecz <[hidden email]>
>>
>> Hi,
>>
>> I have this ugly code:
>>
>> class C(headLen_ : Int, functions: Alphabet[Func]) {
>>  protected val (headLen, tailLen) = {
>>      if (functions.length == 0)
>>        (0, headLen_ * (functions.maxParams - 1) + 1 + headLen_)
>>      else
>>        (headLen_, headLen_ * (functions.maxParams - 1) + 1)
>>    }
>> }
>>
>> Can you recommend me a better way to do it? The only better way I
>> could come up with is to put the initialization in the companion
>> object az a protected method so that I could write this:
>>
>> class C(headLen_ : Int, functions: Alphabet[Func]) {
>>  protected val (headLen, tailLen) = headTailInit(headLen_, functions)
>> }
>>
>> It's much better but I still don't like it. I'm used to initializing
>> everything in the constructor, so that it's clearly separated. I don't
>> know how I could do the same in Scala.
>>
>> Also, I need to call a public method as part of the construction, so
>> in reality it would look more like:
>>
>> class C(headLen_ : Int, functions: Alphabet[Func]) {
>>  protected val (headLen, tailLen) = headTailInit(headLen_, functions)
>>  randomize()
>> }
>>
>> Add a bunch of other methods and it's a complete mess.
>>
>> BR/Szabi
>>
>> --
>> DRM: Digital Restrictions Management -- learn about the dangers at
>> http://www.defectivebydesign.org/what_is_drm
>
>



--
DRM: Digital Restrictions Management -- learn about the dangers at
http://www.defectivebydesign.org/what_is_drm