Question

I'm trying to compile a Scala class file that extends a Java class. Here is the class definition, it's as basic as it gets. On load, in the host application, the object should write "Hello from Scala" to the host's output window, using the superclass's method 'post'.

import com.cycling74.max.MaxObject;

public class FirstClass extends MaxObject{
    public FirstClass{
        post("Hello From Java");
    }
} 

This compiles and runs fine in the application.

Here is my translation to Scala (to be honest I'm not 100% sure I completely understand constructors in Scala):

import com.cycling74.max._

class FirstClass() extends MaxObject {
    super.post("Hello from Scala")
}

But, when I try to compile with scalac, I receive the following error:

dm$ scalac -cp max.jar FirstClass.scala 
FirstClass.scala:3: error: value post is not a member of com.cycling74.max.MaxObject with ScalaObject
    super.post("Hello from Scala")
              ^
one error found

I'm not sure why the compiler is telling me that 'post' is not a member function, I'm certain that I've botched the Scala syntax, but cannot determine what is wrong.

Thanks!

EDIT

here's the output from the Max window, in addition to changing the code as prescribed below, i just added the Scala libs to Max's dynamic loadpath. This is exciting

MXJ System CLASSPATH:
   /Applications/Max 6.1/Cycling '74/java/lib/jitter.jar
   /Applications/Max 6.1/Cycling '74/java/lib/jode-1.1.2-pre-embedded.jar
   /Applications/Max 6.1/Cycling '74/java/lib/max.jar
MXJClassloader CLASSPATH:
   /Applications/Max 6.1/Cycling '74/java/classes/
   /Users/dm/maxmsp/classes
   /Users/dm/maxmsp/jars/jline.jar
   /Users/dm/maxmsp/jars/scala-compiler.jar
   /Users/dm/maxmsp/jars/scala-dbc.jar
   /Users/dm/maxmsp/jars/scala-library.jar
   /Users/dm/maxmsp/jars/scala-partest.jar
   /Users/dm/maxmsp/jars/scala-swing.jar
   /Users/dm/maxmsp/jars/scalacheck.jar
   /Users/dm/maxmsp/jars/scalap.jar
Jitter initialized
Jitter Java support installed
Hello from Scala
Was it helpful?

Solution

Assuming the definitioin of post method in MaxObject as..

public class MaxObject {        
    public static void post(java.lang.String message){
        System.out.println("printing from MaxObject.post :: " + message);
    }
}

you can directly call the post method in scala as -

class FirstClass extends MaxObject {
    MaxObject.post("Hello from Scala")
}

Infact, if you are not compelled to extend the MaxObject.. you can also use it as..

class FirstClass{
    MaxObject.post("Hello from Scala")
}

Consuming it as :

val fc = new FirstClass   //> printing from MaxObject.post :: Hello from Scala
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top