score:2

Accepted answer

i don't think there is a good way to do what you want such that:

  • you can neatly tie all members to an identifier (i.e. refer to the field via swt.x instead of x)
  • have it work both in scala and java.
  • you don't have to manually forward fields.

this is a documented limitation of scala -- see access java base class's static member in scala.

in addition, i don't think the implicit route works either, because you can't treat a java class as a value: how to access a java static method from scala given a type alias for that class it resides in

probably the best way to do what you want is to manually forward the static members you need in my.package.swt:

public class swt extends org.eclipse.swt.swt {
    public static final int finalizetext = 201;
    public static final int parsetext = 202;
    public static int getmousedown() {
        return mousedown;
    }
}

if you only care about automatically forwarding members and not about the other requirements, you can use import:

import org.eclipse.swt.swt._
table.addlistener(mousedown, periodeditlistener)

score:0

i am accepting yuzeh's answer for thoroughness, general applicability, and helpfulness, but here is what i actually did, which is slightly different:

i was very tempted by yuzeh's last suggestion for the sake of uniformity, i.e.

import org.eclipse.swt.swt._
import my.package.swt._

although as my first example snippet above inadvertently shows, swt.none unfortunately is, so bringing it into the local namespace would conflict with option's none.

i think for now i'll just import like:

import org.eclipse.swt.swt
import my.package.{swt => myswt}

if nothing else, it is a bit more clear where the constants are coming from. there, i talked myself into believing this is better :).


Related Query

More Query from same tag