[swift-users] Import C functions as subscript methods

Michael Ilseman milseman at apple.com
Mon Jul 11 16:25:21 CDT 2016


Subscript is not yet implemented. I will hopefully get that in time for Swift 3. Requiring “newValue” isn’t as well for properties, though it’s coincidentally allowed as a perfectly valid argument label for the setter.

> On Jul 11, 2016, at 4:59 AM, Martin R via swift-users <swift-users at swift.org> wrote:
> 
> Minimal progress: With
> 
>    // Import as subscript
>    float Point3DGetPointAtIndex(Point3D point, int idx)
>    __attribute__((swift_name("getter:Point3D.subscript(self:_:)")));
>    void Point3DSetPointAtIndex(Point3D point, int idx, float val)
>    __attribute__((swift_name("setter:Point3D.subscript(self:_:newValue:)")));
> 
> the compiler does not issue warnings anymore. However, the functions are still not imported as subscript getter/setter. (Tested with Xcode 8 beta 2).
> 
> 
>> On 11 Jul 2016, at 11:48, Martin R <martinr448 at gmail.com> wrote:
>> 
>> SE-0044 ( https://github.com/apple/swift-evolution/blob/master/proposals/0044-import-as-member.md ) describes how to annotate C functions with the swift_name attribute to tell the Swift compiler how an API should be imported. There is an "Amendment: Also allow for importing as subscript." but I could not make that part work.
>> 
>> This compiles and works as expected:
>> 
>>   typedef struct Point3D {
>>       float x;
>>       float y;
>>       float z;
>>   } Point3D;
>> 
>>   struct Point3D createPoint3D(float x, float y, float z)
>>   __attribute__((swift_name("Point3D.init(x:y:z:)")));
>> 
>>   // Import as method
>>   struct Point3D rotatePoint3D(Point3D point, float radians)
>>   __attribute__((swift_name("Point3D.rotate(self:radians:)")));
>> 
>> but 
>> 
>>   // Import as subscript
>>   float Point3DGetPointAtIndex(int idx, Point3D point)
>>   __attribute__((swift_name("getter:subscript(_:self:)")));
>>   void Point3DSetPointAtIndex(int idx, Point3D point, float val)
>>   __attribute__((swift_name("setter:subscript(_:self:newValue:)")));
>> 
>> causes compiler warnings
>> 
>>   warning: 'swift_name' attribute for 'subscript' must take a 'self:' parameter [-Wswift-name-attribute]
>> 
>> and the functions are not imported as a subscript getter/setter.
>> 
>> Is that not yet implemented, or is my syntax wrong?
>> 
>> Regards, Martin
>> 
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users



More information about the swift-users mailing list