musictheory / NilScript

Objective-C-style language superset of JavaScript with a tiny, simple runtime
Other
50 stars 5 forks source link

Categories #55

Closed ChristopherBruno78 closed 8 years ago

ChristopherBruno78 commented 9 years ago

I wonder if there is any plan to implement Objective-C Categories?

IngwiePhoenix commented 9 years ago

Just curios, but would you mind sharing a pseudo-example how that would possibly look/work in OJ and what could be done with it?

ChristopherBruno78 commented 9 years ago

Categories are a core feature of objective-c, would be nice to use them with the oj compiler. Right now you get an compile error. What should happen is the methods get added to an existing class (if the class doesnt existing, you get a runtime exception)

iccir commented 9 years ago

This should be doable, syntax would be similar to Obj-C:

@implementation Foo (CategoryName)
@end

Which would have an AST node similar to:

{ type: Syntax.OJClassImplementation, id: "Foo", category: "CategoryName", body: ... }

Likely handleClassImplementation in the generator would be modified to call some kind of _registerCategory function in this case rather than _registerClass at runtime.

iccir commented 8 years ago

6072f88 adds the ability to define categories which add methods, similar to objc 1.

The following are unsupported:

Like Obj-C, defining a method that already exists is undefined behavior.