spencersalazar / chuck

28 stars 8 forks source link

Add constructors to class definitions #32

Closed heuermh closed 8 months ago

heuermh commented 10 years ago

Often state within fields of a class must be initialized properly before an instance of a class can be used by callers. Typically this initialization is performed in a constructor.

Constructor declarations http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8

Since ChucK does not provide constructors library authors have to rely on a pattern of static factory methods, which instantiate and return a new instance of another class. They do not prevent callers from creating instances of that class with improperly initialized fields however.

For example, the LiCK class Scales contains several static methods that instantiate and return instances of Scale.

Scales.ck, line 226 https://github.com/heuermh/lick/blob/master/Scales.ck#L226

Scales.ck, line 233 https://github.com/heuermh/lick/blob/master/Scales.ck#L233

etc.

It would be preferable to instantiate these with constructors on the subclasses of Scale themselves.

gewang commented 8 months ago

as of 1.5.2.0 chuck supports constructors! see release notes https://github.com/ccrma/chuck/blob/2a42d9e1a8be85cdf4526eb8817ce69683a5642e/VERSIONS#L5-L56

heuermh commented 8 months ago

Thank you, @gewang