Error making class in SuperCollider

I’m having issues with the basic code provided in the “Writing Classes” help file ( Writing Classes | SuperCollider 3.11.1 Help (sccode.org)). When I try to use the following code:

MyClass {

instanceMethod { | argument |
    this.anotherInstanceMethod(argument)
}

anotherInstanceMethod { | argument |
    "hello instance".postln
}

}

I get the error:
ERROR: syntax error, unexpected NAME, expecting ‘}’
in interpreted text
line 7 char 25:

  anotherInstanceMethod { | argument |
  ^^^^^^^^^^^^^^^^^^^^^
      "hello instance".postln

ERROR: Command line parse failed
→ nil

I assumed that perhaps the problem is not having semicolons after the method definitions, but when I change the code to this:

MyClass {

instanceMethod { | argument |
    this.anotherInstanceMethod(argument)
};

anotherInstanceMethod { | argument |
    "hello instance".postln
};

}

I get this new error:

ERROR: Class not defined.
in interpreted text
line 1 char 7:

MyClass {


→ nil

Am I making some sort of basic mistake here? Or have the sclang specifications changed since the “Writing Classes” article was written? I’ve tried this on two computers and it doesn’t work on either of them.

The code itself is syntactically correct and should work as expected. The error that you’re seeing is the result of trying to define a Class at runtime, which sclang doesn’t support.

Near the top of the “Writing Classes” helpfile that you linked, there’s a note that reads (emphasis mine):

NOTE: Class definitions are statically compiled when you launch SuperCollider or “recompile the library.” This means that class definitions must be saved into a file with the extension .sc, in a disk location where SuperCollider looks for classes. Saving into the main class library (SCClassLibrary) is generally not recommended. It’s preferable to use either the user or system extension directories.

Platform.userExtensionDir; // Extensions available only to your user account

Platform.systemExtensionDir; // Extensions available to all users on the machine

It is not possible to enter a class definition into an interpreter window and execute it.

So in order to use the MyClass definition from the example:

  1. evaluate Platform.userExtensionDir and note the path that’s returned. For Linux this should be something like “~/.local/share/SuperCollider/Extensions”. For macOS iirc it’s “~/Library/Application Support/SuperCollider/Extensions”.
  2. create a file with the extension “.sc” (NOT “.scd”) at that path, say “MyClass.sc” for example.
  3. copy your Class definition to this classfile and save the file.
  4. recompile the sclang Class library (ctrl+shift+L in SCIDE/Linux).

Now you should be able to create an instance of MyClass and call its methods:

~foo = MyClass.new // should return "a MyClass"

~foo.instanceMethod // should post "hello instance"

Note also that after any edits to Class definitions, you will need to save the classfile and recompile in order to see the results of those edits.

Finally, if you’re curious about Class syntax, the best resource by far is the source code of sc’s Class library itself. I recommend getting in the habit of using the IDE to look up the implementation (ctrl+I) of anything you’re curious about.

1 Like

You already got your answer from @catniptwinz it seems but I just wanna chime in here and recommend @elifieldsteel’s tutorial on this subject:

1 Like

ah, thanks! I have watched Eli Fieldsteel’s tutorial on classes but it was a while ago. I either forgot or overlooked the detail about putting the class files in the right directory before starting SuperCollider.