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

Re: How to force initialization of inner objects?

4 replies
Seth Tisue
Joined: 2008-12-16,
User offline. Last seen 34 weeks 3 days ago.

>>>>> "Sebastien" == Sebastien Bocq <sebastien.bocq@gmail.com> writes:

Sebastien> Thanks you all for the advice, it works now. javap shows
Sebastien> that the field corresponding to the inner object is
Sebastien> initialized with a new instance of Funny$name in the name()
Sebastien> method is invoked. Therefore I used reflection to invoke all
Sebastien> the methods that return a specific type inherited by my
Sebastien> inner objects like in the sample below.

Now I'm kind of curious what problem you had, that you decided you had
no choice but to do this...? The whole thing seems baroque and best
avoided if there's some way to avoid it.

Sebastien Bocq
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
Re: How to force initialization of inner objects?
abstract class Whatever extends ComponentVar

2008/12/19 Sebastien Bocq <sebastien.bocq@gmail.com>
I want to discover dynamically the name and description of specific inner fields declared in a "Component" class but with as little extra effort as possible from the programmer perspective. With:

trait ComponentVar {
  def description:String
}

abstract class Whatever

class MyComponent extends Component {

   object x extends Whatever {
       def description = "Hello, I'm a variable used to do this cool thing in MyComponent"
   }
}

"x" and his description are registered automatically to the local store in the parent "Component" class (e.g. a map) because they extend ComponentVar and the compiler can check that the description is not missing. At the time using "object" seemed easy but I didn't expect it would turn out to be lazy too...

What bothers me most with my approach is that I need to create instance of MyComponent because I cannot access the description string statically. Ideally I would need something like ComponentVar["Hello, this is a description of ComponentVar in MyComponent"] to reason on the type itself but this is not possible with Scala generics.

Any suggestion is welcome,
Sébastien

2008/12/19 Seth Tisue <seth@tisue.net>

>>>>> "Sebastien" == Sebastien Bocq <sebastien.bocq@gmail.com> writes:

 Sebastien> Thanks you all for the advice, it works now. javap shows
 Sebastien> that the field corresponding to the inner object is
 Sebastien> initialized with a new instance of Funny$name in the name()
 Sebastien> method is invoked. Therefore I used reflection to invoke all
 Sebastien> the methods that return a specific type inherited by my
 Sebastien> inner objects like in the sample below.

Now I'm kind of curious what problem you had, that you decided you had
no choice but to do this...?  The whole thing seems baroque and best
avoided if there's some way to avoid it.

--
Seth Tisue / http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/



Sebastien Bocq
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
Re: How to force initialization of inner objects?
I want to discover dynamically the name and description of specific inner fields declared in a "Component" class but with as little extra effort as possible from the programmer perspective. With:

trait ComponentVar {
  def description:String
}

abstract class Whatever

class MyComponent extends Component {

   object x extends Whatever {
       def description = "Hello, I'm a variable used to do this cool thing in MyComponent"
   }
}

"x" and his description are registered automatically to the local store in the parent "Component" class (e.g. a map) because they extend ComponentVar and the compiler can check that the description is not missing. At the time using "object" seemed easy but I didn't expect it would turn out to be lazy too...

What bothers me most with my approach is that I need to create instance of MyComponent because I cannot access the description string statically. Ideally I would need something like ComponentVar["Hello, this is a description of ComponentVar in MyComponent"] to reason on the type itself but this is not possible with Scala generics.

Any suggestion is welcome,
Sébastien

2008/12/19 Seth Tisue <seth@tisue.net>
>>>>> "Sebastien" == Sebastien Bocq <sebastien.bocq@gmail.com> writes:

 Sebastien> Thanks you all for the advice, it works now. javap shows
 Sebastien> that the field corresponding to the inner object is
 Sebastien> initialized with a new instance of Funny$name in the name()
 Sebastien> method is invoked. Therefore I used reflection to invoke all
 Sebastien> the methods that return a specific type inherited by my
 Sebastien> inner objects like in the sample below.

Now I'm kind of curious what problem you had, that you decided you had
no choice but to do this...?  The whole thing seems baroque and best
avoided if there's some way to avoid it.

--
Seth Tisue / http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/


Seth Tisue
Joined: 2008-12-16,
User offline. Last seen 34 weeks 3 days ago.
Re: How to force initialization of inner objects?

>>>>> "Sebastien" == Sebastien Bocq <sebastien.bocq@gmail.com> writes:

Sebastien> I want to discover dynamically the name and description of
Sebastien> specific inner fields declared in a "Component" class but
Sebastien> with as little extra effort as possible from the programmer
Sebastien> perspective.

It still seems to me that you're describing a solution you've already
chosen, rather than describing the problem you're trying to solve.

It's hard to be sure without knowing the context, but I think instead of
making your "ComponentVars" fields in the first place, I would just
store them in some sort of data structure?

Sebastien Bocq
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
Re: How to force initialization of inner objects?
2008/12/19 Seth Tisue <seth@tisue.net>
It still seems to me that you're describing a solution you've already
chosen, rather than describing the problem you're trying to solve.

You're right, it is mostly because I ended up playing with the features of the language rather than focusing on my specific problem, initially I was looking after type annotation... I just discovered inner objects and path-dependent types, mixed up these concepts along the way, and ended up with something completely baroque :)

It is better I stop this thread here, I don't want to waste your time.

Thanks a lot for your patience!
Sebastien

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