[SOLVED] Norns SC Engine Array Declaration Error

Hi,

Have an odd issue with a SuperCollider engine I’m developing.

I can’t seem to declare an array variable in the engine.

I’ve tried

var variablename = Array.new(10);

and

var variablename = [];

In either case, I get a supercollider error, on Norns startup.

I’ve tried restarting SC from Maiden, and I can see a parse error in the log.

I’ve commented out everything else in the engine apart from the basic boilerplate code.

If I comment out the array declaration, I don’t get the error.

Here’s the error message that shows up in Maiden:

ERROR: file '/home/we/dust/code/bline/lib/Engine_Bline.sc' parse failed
error parsing
	Found 716 primitives.
	Compiling directory '/usr/share/SuperCollider/SCClassLibrary'
	Compiling directory '/usr/share/SuperCollider/Extensions'
	Compiling directory '/home/we/.local/share/SuperCollider/Extensions'
	Compiling directory '/home/we/norns/sc/core'
	Compiling directory '/home/we/norns/sc/engines'
WARNING: Could not open directory: '/home/we/norns/sc/ugens'
	To resolve this, either create the directory or remove it from your compilation paths.
	Compiling directory '/home/we/dust'
ERROR: syntax error, unexpected CLASSNAME
  in file '/home/we/dust/code/bline/lib/Engine_Bline.sc'
  line 24 char 24:
  	var activeFreqs = Array.new(10);
                     ^^^^^
  
-----------------------------------
ERROR: file '/home/we/dust/code/bline/lib/Engine_Bline.sc' parse failed
error parsing

Here’s the stripped-down engine:

// CroneEngine_Bline
// Crappy 303
Engine_Bline : CroneEngine {

	var pg;

	// Active notes array
	var activeFreqs = Array.new(10);

	*new { arg context, doneCallback;
		^super.new(context, doneCallback);
	}

	alloc {

		pg = ParGroup.tail(context.xg);


	} // end alloc

} // end class

Any advice?

I should say I’m using the latest Norns system available on a Fates.

Posting code and the precise error message is really critical when raising questions like this.

I’m by no means a SC expert but the first thing that comes to mind and often trips me up is that one cannot declare variable after the first statement/expression in the function.

1 Like

var variablename = #[]

should work

but i would actually just initialize later, in *new or alloc or whenever needed, bc/ there is no convenient sytnax for what you actually want which is #[0, 0, 0, 0, 0, 0, 0, 0] or whatevr

https://depts.washington.edu/dxscdoc/Help/Guides/WritingClasses.html

Initializing variables directly

In a variable declaration, variables can be directly initialized. Only Literals may be used to initialize variables this way. This means that it is not possible to chain assignments (e.g. var x = 9; var y = x + 1).

MyClass {

classvar all = #[];

var x = 8;

var y = #[1, 2, 3];

}

also
https://depts.washington.edu/dxscdoc/Help/Reference/Literals.html

2 Likes

Fair point. I wrote the original post on my phone, which is fiddly, with the intention of adding code snippets when I got back on my laptop. Which I’ve now done.

here is where its saying it cant construct an instance of a class here (at class compilation time)


Engine_Bline : CroneEngine {
	var pg;
	var activeFreqs;

	alloc {

		pg = ParGroup.tail(context.xg);
        activeFreqs = Array.new(10);

	} // end alloc

} // end class
1 Like

Thanks for the super fast response, @zebra.

Can you explain why this throws an error in an engine, but works when I declare the Array in a standalone SC file?

I realise an Engine is a class, but shouldn’t it be possible to declare an instance of another class inside a class declaration?

no it doesnt. cant do this in any class definition

Borf {
	var zorf = Array.new;
}
ERROR: syntax error, unexpected CLASSNAME
  in file '/home/emb/.local/share/SuperCollider/Extensions/Borf.sc'
  line 2 char 17:

  	var zorf = Array.new;
              ^^^^^
  }

you can only construct objects at runtime (e.g. from .scd script or from class methods), not at class compilation time (in top scope of class definition.)

that makes sense, right? class compilation happens. sclang class definition code is rendered to bytecode. literals can just be represented in bytecode. but it doesnt make sense for a class definition to construct an object, that requires the interpreter to be running.

instead, construct objects from within in your class’s methods, which are executed by the runtime interpreter and not the class compiler.

2 Likes

Gotcha, cool :slight_smile:

Thanks very much for the tips, @zebra;

All working now!