logo       

Re: self-referential static members: msg#00028

lang.scala

Subject: Re: self-referential static members

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




<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise