chuck icon indicating copy to clipboard operation
chuck copied to clipboard

Allow a class to implement more than one interface or extend more than one class

Open heuermh opened this issue 6 years ago • 0 comments

Often a single class will compose several different aspects of functionality.

For example, the LiCK class Sample can be used with several other APIs in the LiCK library. It has its own direct API (play() method) and it also implements several of the LiCK functor interfaces (Procedure run(), IntProcedure run(int), IntIntProcedure run(int, int), FloatProcedure run(float), and FloatFloatProcedure run(float, float)).

In Java and other object-oriented languages, class Sample could implement all of these interfaces

public class Sample
  implements Procedure, IntProcedure,
IntIntProcedure, FloatProcedure, FloatFloatProcedure
{
  //...
}

ChucK does not provide interfaces, only classes. This would also be possible with multiple class inheritance, but ChucK does not provide that either. Thus Sample must be implemented with the hack as described here

Sample.ck, line 97 https://github.com/heuermh/lick/blob/master/Sample.ck#L97

and then callers must use one of the asXXX() methods instead of relying on the type system

euclidMidiTr808.ck, line 28 https://github.com/heuermh/lick/blob/master/examples/euclidOscTr808.ck#L28

Migrated from https://github.com/spencersalazar/chuck/issues/30

heuermh avatar Jan 16 '18 17:01 heuermh