[swift-users] Import C functions as subscript methods

Martin R martinr448 at gmail.com
Mon Jul 11 04:48:30 CDT 2016


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