[swift-users] Import C functions as subscript methods

Martin R martinr448 at gmail.com
Mon Jul 11 06:59:37 CDT 2016


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
> 



More information about the swift-users mailing list