musictheory / NilScript

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

Support protocols and typed arrays #31

Closed iccir closed 9 years ago

iccir commented 9 years ago

Add support for protocols (id<ProtocolName>) for method parameters, ivars, @properties, etc. This will be for documentation purposes at first. With the addition of union types in TypeScript, we could also possibly add this to the typechecker.

Additionally, the same parsing code can handle typed arrays: Array<String> for an Array of String objects.

I'm hesitant to add generics or expose TypeScript's generic types/functions, but I think making a one-time exception for Array is worth it. Since there is no way for an oj class to subclass the native Array type, the angle bracket syntax on Array will never indicate a protocol. This will need to be evaluated for Set in ECMAScript 6. Also, if type annotations make it into ECMAScript 7, we might need to support that syntax in addition for typed arrays.

iccir commented 9 years ago

To be specific, the following currently fail to parse:

- (void) aMethod:(id<Foo>)arg { }
@property id<Foo, Bar> property;
@property Foo<Foo<Bar>> property;
@property Foo<Baz, Foo<Bar>> property;

This issue is simply about making these cases succeed at parse time. The oj compiler will then immediately throw out this type information (the only time type information affects codegen is for numeric/boolean ivars, which are initialized to 0/false. These are all object types, so as ivars they would be initialized to null).

The part above regarding Array typing is for the experimental type checker. As everything else with the type checker, it's subject to change ;)