Objects, Functions, OOP Concepts

I’ve done some OOP coding in C++, Java, JavaScript and PHP, so I’m familiar with the basics of object-orientated programming.

All the SC documentation says SuperCollider is object-based. However, I only see mentions user-created classes in the context of SuperCollider extensions, but lots about creating and using functions.

Since SC is object-based, I assume functions are also objects. I don’t see anything in the tutorial docs I’ve seen so far about using functions as objects, however (ie treating a function declaration as a class definition, and creating instances, with their own encapsulated state, as one would do with OOP).

The only exception to this I see commonly is SynthDef, which seems to commonly be used in this way.

If, for example, I wanted to create, say, a kind of envelope, that I wanted to use in multiple places, passing different arguments, I’d obviously want to re-use the same code in some way.

The obvious way to do this would seem to be to create a function for it in the global scope, with a number of arguments and internal, local variables, and assign the function to a variable.

What would be the best way to proceed from there, though?

Would I just reuse the function by calling it, passing different values as arguments, functional programming style?

This would seem at odds with the object-based approach SC seems to encourage.

What would be the OOP way to approach this?

Sorry for the quite general question. I’m just trying to get my head around some basic concepts before I proceed too far down a coding dead-end.

Apologies also if my language is a bit on the “wooly” side. I’m entirely self-taught when it comes to writing code, so I’m not always able to be as precise as I’d like.

A function is an object in the sense that you can store it in a variable or pass it to a method, just like a number or string. Traditional C or Pascal functions are not first class objects in that way.

SC functions don’t work that way.

If you want a class definition, it needs to be a class definition. A function definition simply isn’t a class definition.

Exactly.

Not really. Functions being objects means you still use them like functions.

hjh

1 Like

Hi!

See
http://doc.sccode.org/Guides/WritingClasses.html

I have more thoughts, but no time just now

Cheers
eddi
https://soundcloud.com/all-n4tural
https://alln4tural.bandcamp.com

Also releated to Prototyping techniques, see:

and more in general:
https://scsynth.org/search?q=proto
:slight_smile:

1 Like

Thanks both for getting back to me. Lots to digest there :slight_smile:

since functions are first-class objects, a bunch of functional programming technics are available in sclang too fwiw - .inject, .collect, list comprehensions, currying - and there is an FPLib quark that tries to bolt more Haskellisms on as well…

1 Like

Good to know, @semiquaver.

Thanks All. I will try and absorb all this :slight_smile: