|
View:
New views
7 Messages
—
Rating Filter:
Alert me
|
|
|
Traits and importsHi, I'm writing my first Scala library and tried importing a package in the body of a trait hoping that the import statement would be mixed in to another class the same way that members are but it seems not to work. So, for example, I have: trait OpenGL extends javax.media.opengl.GLEventListener { import javax.media.opengl._ override def display(draw: GLAutoDrawable) { render(draw.getGL()) } def render(gl: GL) { } //... } import scala.swing._ class Screen extends MainFrame Then in my GUI application I have: import scala.swing._ object MyApp extends SimpleGUIApplication { def top = new Screen with OpenGL { override render(gl: GL) { // <- Won't Compile } } } But this won't compile unless I explicitly import the javax.media.opengl package. Am I doing something wrong or is mixing in an import from a trait not possible? Thanks, John Senior Technician Digital Media Workshop Middlesex University London, UK |
|
|
Re: Traits and importsAFAIK, not possible. It would be useful for some stuff, but, then, it would make it much more difficult to control the scope of imports.
On Thu, Nov 5, 2009 at 10:40 AM, John Cox <johnedwardcox@...> wrote:
-- Daniel C. Sobral Veni, vidi, veterni. |
|
|
Re: Traits and importsOK, thanks for answering. It's a shame though. I would like to keep the client code clean by having some of the imports declared in the trait. Requiring the client to work out all the imports used in a trait just to keep the compiler happy seems like extra work for nothing -- (what if you don't have the source?) Question for everyone: Does the idea of mixing in imports from traits seem useful generally, or is the extra complexity not worth it? Regards, John From: Daniel Sobral <dcsobral@...> To: John Cox <johnedwardcox@...> Cc: scala-user@... Sent: Thu, November 5, 2009 2:33:17 PM Subject: Re: [scala-user] Traits and imports AFAIK, not possible. It would be useful for some stuff, but, then, it would make it much more difficult to control the scope of imports. On Thu, Nov 5, 2009 at 10:40 AM, John Cox <johnedwardcox@...> wrote:
-- Daniel C. Sobral Veni, vidi, veterni. |
|
|
Re: Traits and importsBut you would need to import only the packages that you are using not the ones the trait is using internally.
So in your case you would need GL since you are reffering to it from your class all other imports shouldn't matter as far as I know. And this would be perfectly suitable since you would need to know the type anyway in order to handle it properly. So no need to know internas of your trait. Regards Stefan 2009/11/5 John Cox <johnedwardcox@...>
|
|
|
Re: Traits and importsHi Stefan, Yes, its true that I don't have to know the internal implemention of the trait, just the public method parameters &c -- but when mixing in multiple traits this is still potentially a lot of imports. Anyway, now I think about it the obvious reason why this doesn't work is that the compiled trait class file doesn't contain the imports as declared in the source so there is no straight forward way for client code to know which imports are potentially in scope other than parse all the method signatures for the trait -- obviously not worth the compiler's time. Same basic reason a Java subclass has to import the same packages as its parent class. Thanks, John From: Stefan Langer <mailtolanger@...> To: John Cox <johnedwardcox@...> Cc: scala-user@... Sent: Thu, November 5, 2009 4:19:47 PM Subject: Re: [scala-user] Traits and imports But you would need to import only the packages that you are using not the ones the trait is using internally. So in your case you would need GL since you are reffering to it from your class all other imports shouldn't matter as far as I know. And this would be perfectly suitable since you would need to know the type anyway in order to handle it properly. So no need to know internas of your trait. Regards Stefan 2009/11/5 John Cox <johnedwardcox@...>
|
|
|
Re: Traits and importsTry to define a member type GL = javax.media.opengl.GL in trait,
Or, if you'd like to import a object, define a member val OBJECT = .... in trait I use this way in my projects. trait OpenGL extends javax.media.opengl.GLEventListener { import javax.media.opengl.GLAutoDrawable // here member type GL type GL = javax.media.opengl.GL override def display(draw: GLAutoDrawable) { render(draw.getGL()) } def render(gl: GL) { } //... } import scala.swing._ class Screen extends MainFrame ========= import scala.swing._ object MyApp extends SimpleGUIApplication { def top = new Screen with OpenGL { override render(gl: GL) { } } } On Thu, Nov 5, 2009 at 8:40 PM, John Cox <johnedwardcox@...> wrote: > > Hi, > > I'm writing my first Scala library and tried importing a package in the body > of a trait > hoping that the import statement would be mixed in to another class the > same way that members are but it seems not to work. So, for example, I > have: > > trait OpenGL extends javax.media.opengl.GLEventListener { > import javax.media.opengl._ > > override def display(draw: GLAutoDrawable) { > render(draw.getGL()) > } > > def render(gl: GL) { > } > > //... > } > > > import scala.swing._ > > class Screen extends MainFrame > > Then in my GUI application I have: > > import scala.swing._ > > object MyApp extends SimpleGUIApplication { > def top = new Screen with OpenGL { > override render(gl: GL) { // <- Won't Compile > > } > } > } > > But this won't compile unless I explicitly import the javax.media.opengl > package. > Am I doing something wrong or is mixing in an import from a trait not > possible? > > Thanks, > > John > Senior Technician > Digital Media Workshop > Middlesex University > London, UK > > > > |
|
|
Re: Traits and importsI was going to say exactly what Caoyuan said. Something that sometimes I found useful, when I intend that the classes the mix in one trait automatically have visibility to other classes, is define types for each class I intend to make visible, this way, you control exactly WHICH classes are "automatically imported" for your client classes, and also the name!, which is very useful!.
Cheers On Thu, Nov 5, 2009 at 3:46 PM, Caoyuan <dcaoyuan@...> wrote: Try to define a member type GL = javax.media.opengl.GL in trait, |
| Free embeddable forum powered by Nabble | Forum Help |