skip icon indicating copy to clipboard operation
skip copied to clipboard

Extensions on Array that are not referenced in the compile unit may fail to `import skip.lib.Array`, thus resulting in the wrong Array type being used

Open marcprux opened this issue 1 year ago • 3 comments

If you have a Swift file with just a single extension on Array (or Collection):

public extension Array {
    func jsonString() -> String? {
        guard let data = try? JSONSerialization.data(withJSONObject: self, options: .fragmentsAllowed) else {
            return nil
        }
        return String(data: data, encoding: .utf8) ?? nil
    }
}

The resulting Kotlin will not add the explicit import skip.lib.Array line, thus resulting in the extension being incorrectly added to the kotlin.Array, and thereby preventing the extension from being used in the correct way.

For example, the above code will result in the javap -c bytecode:

public static final <Element> java.lang.String jsonString(Element[]);

If the Array type is referenced anywhere in the file, then we will add the explicit import skip.lib.Array line to the transpiled Kotlin, thereby resulting in the expected compiled code:

public static final <Element> java.lang.String jsonString(skip.lib.Array<Element>);

The symptom will be that the extension will simply not be found in any code that references the extension. The workaround is to simply reference the Array type somewhere in the compile unit, which will trigger Skip to add the explicit skip.lib.Array import. E.g., this will be sufficient:

private func doSomething(array: Array<Int>) { }

marcprux avatar May 30 '24 13:05 marcprux

Test case FeatureSupportTests.testExtendArrayShouldImportArray()

marcprux avatar May 30 '24 13:05 marcprux

This should now be fixed in skip 0.8.50. Use File->Packages->Update to Latest to get the latest transpiler with the fix

aabewhite avatar Jun 12 '24 20:06 aabewhite

Thanks Abe, confirmed working.

Cheers, Ben

benford-servicem8 avatar Jul 09 '24 07:07 benford-servicem8