skip icon indicating copy to clipboard operation
skip copied to clipboard

Complex generic constraints can't be overridden with `SKIP DECLARE`

Open dfabulich opened this issue 7 months ago • 2 comments

Regarding https://github.com/skiptools/skip-ui/pull/210 I was unable to get this declaration to transpile:

// SKIP DECLARE: public fun <K: PreferenceKey<V>, V: Any> View.onPreferenceChange(key: KClass<K>, perform: (V) -> Unit): View
public func onPreferenceChange<K>(
        _ key: K.Type = K.self,
        perform action: @escaping (
            K.Value
        ) -> Void
    ) -> some View where K : PreferenceKey, K.Value : Equatable {

The transpiler kept objecting to the K.Value : Equatable clause of the where. That would be fine if the SKIP DECLARE comment would allow me to use an alternate declaration, but Skip refused to transpile this code even with SKIP DECLARE.

IMO, SKIP DECLARE should make the transpiler stop complaining about the declaration.

dfabulich avatar May 21 '25 06:05 dfabulich

As mentioned in the similar issue report at https://github.com/orgs/skiptools/discussions/514, the problem is that Skip errors out before it even processes the SKIP DECLARE. But you might be able to externalize generic constraint in a typealias to work around it. Something like this:

#if !SKIP
typealias PreferenceKeyOf<K: PreferenceKey> = K where K.Value : Equatable
#else
typealias PreferenceKeyOf = PreferenceKey
#endif

func onPreferenceChange<K>(_ key: K.Type = K.self, perform action: @escaping (PreferenceKeyOf<K>.Value) -> Void) -> some View {
}

marcprux avatar Oct 20 '25 19:10 marcprux

Is this something y'all could fix?

If not, could it be documented near SKIP DECLARE? https://skip.tools/docs/platformcustomization/#skip-comments

dfabulich avatar Oct 21 '25 04:10 dfabulich