home/ atoms/ sc-class-method-instance-method-caret

SuperCollider methods do not auto-return their last expression; the caret (^) designates the return value

Unlike functions, SuperCollider methods do not automatically return the value of their last expression — you must explicitly precede the intended return value with the caret symbol (^). Inside a method body, this refers to the receiver of the message (for 144.tempodur, this is 144). Method definitions live in class-extension files using the + ClassName { methodName { … } } syntax; class methods are prefixed with * (e.g. *new) while instance methods have no prefix. After editing an extension file you must recompile the class library before the method is available. Forgetting the caret makes the method return the receiver itself, not the computed value — a common beginner bug.

Examples

+ SimpleNumber { tempodur { // instance method: no * var tempo = this; // receiver ^(60/tempo); // ^ designates return value } }

Assessment

A student writes a method that computes a result but omits the ^ caret. What does the method return instead? Why does SC not auto-return the last line in methods as it does in functions?

“Methods don't automatically return the last line the way functions do. Instead, we need to explicitly designate the value that this method returns. We precede the desired output with the caret symbol.”
corpus · supercollider-tutorials-full-transcripts-and-code-eli-fields · chunk 102