self-referential static members

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

self-referential static members

Jason Kinzer
Hi,

Regarding static members: it's my understanding they can be viewed as instances of a singleon object. However, I'm not able to acheive the effect I want in the following case, perhaps because it's more akin to viewing them as members of a metaclass. Here is an example in Java coming from the 'SimpleOrm' library:

public class Employee extends SRecordInstance implements java.io.Serializable {
    
    public static final SRecordMeta meta =
        new SRecordMeta(Employee.class, "XX_EMPLOYEE");
    // ie. SRecord objects describe SRecordInstances
    
    public static final SFieldString EMPEE_ID =
        new SFieldString(meta, "EMPEE_ID", 20, SCon.SFD_PRIMARY_KEY);

     ...
}

The salient feature of this is that it's necessary to to refer to Employee as a static member inside the employee class itself. How can I express this directly in Scala? Apologies in advance if this a failure on my part to RTM.

Thanks very much for your time.

Jason
Reply | Threaded
Open this post in threaded view
|

Re: self-referential static members

Nikolay Mihaylov
Hi

On Wed, 2006-02-08 at 17:57 -0600, Jason Kinzer wrote:

> Regarding static members: it's my understanding they can be viewed as
> instances of a singleon object. However, I'm not able to acheive the
> effect I want in the following case, perhaps because it's more akin to
> viewing them as members of a metaclass. Here is an example in Java
> coming from the 'SimpleOrm' library:
>
> public class Employee extends SRecordInstance implements
> java.io.Serializable {
>    
>     public static final SRecordMeta meta =
>         new SRecordMeta(Employee.class, "XX_EMPLOYEE");
>     // ie. SRecord objects describe SRecordInstances
>    
>     public static final SFieldString EMPEE_ID =
>         new SFieldString(meta, "EMPEE_ID", 20, SCon.SFD_PRIMARY_KEY);
>
>      ...
> }

It's not clear to me what you are unable to achieve. I would have
written sth like:

object Employee {
  val meta =
    new SRecordMeta(Class.forName("mypackage.Employee"), XX_EMPLOYEE)
  val EMPEE_ID =
    new SFieldString(meta, "EMPEE_ID", 20, SCon.SFD_PRIMARY_KEY)
}

[serializable]
class Employee extends SRecordInstance {
  import Employee._;
  ...
}

Wouldn't that be equivalent to the Java code above?

> The salient feature of this is that it's necessary to to refer to
> Employee as a static member inside the employee class itself. How can
> I express this directly in Scala? Apologies in advance if this a
> failure on my part to RTM.

What do you mean by "Employee as a static member?" And what difference
does it make where you want to refer to it from? My suggestion is that
you are missing the Employee.class syntax but for all practical
applications Class.forName() provides equivalent behaviour. The only
situation where you can not do without special syntax is attributes
(annotations in Java parlance) which can sometimes have Class values as
their parameters. The problem with those is that the specifed class will
have to be identified at compile time, not at runtime when you can
simply call forName.

I hope this answers your questions.

Nikolay

Reply | Threaded
Open this post in threaded view
|

Re: self-referential static members

Jason Kinzer
On 2/9/06, Nikolay Mihaylov <[hidden email]> wrote:
Hi

It's not clear to me what you are unable to achieve. I would have
written sth like:

object Employee {
val meta =
   new SRecordMeta(Class.forName("mypackage.Employee"), XX_EMPLOYEE)
val EMPEE_ID =
   new SFieldString(meta, "EMPEE_ID", 20, SCon.SFD_PRIMARY_KEY)
}

[serializable]
class Employee extends SRecordInstance {
import Employee._;
...
}

Wouldn't that be equivalent to the Java code above?
 
Yes, that's exactly it. I clearly didn't fully grasp the mixin + singleton-object idiom for encoding static members, and should have realized I didn't need compile-time identification of the class (.forName works perfectly).
 
Many thanks for straightening me out.

Jason