UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

ExpressibleBy#Literal with parameter pack "Cannot convert value of type '#ExpressibleType' to expected argument type '#Type'"

Forums > Swift

@m_sh  

I'm trying to create a generic function to apply some image filters.

The applyFilters(_:) function uses Parameter Pack of ImageFilter.

Calling applyFilters(_:) with static functions that take a type literal I got the error:

Cannot convert value of type #ExpressibleType to expected argument type #Type.

But creating an object directly from ImageBlurFilter.Radius or ImageExposureAdjust.Scalar work fine.

protocol ImageFilter {

    associatedtype Processor: ImageFilterProcessor
    /**/
}

protocol ImageFilterProcessor { /**/ }

struct ImageBlurFilter: ImageFilter {

    init(radius: Radius) { /**/ }

    struct Processor: ImageFilterProcessor { /**/ }

    struct Radius: ExpressibleByIntegerLiteral {

        init(integerLiteral value: Int) { /**/ }
    }
}

struct ImageExposureAdjust: ImageFilter {

    init(scalar: Scalar) { /**/ }

    struct Processor: ImageFilterProcessor { /**/ }

    struct Scalar: ExpressibleByFloatLiteral {

        init(floatLiteral value: Double) { /**/ }
    }
}

extension ImageFilter where Self == ImageBlurFilter {

    static func blur(radius: ImageBlurFilter.Radius) -> ImageBlurFilter {
        ImageBlurFilter(radius: radius)
    }
}

extension ImageFilter where Self == ImageExposureAdjust {

    static func exposureAdjust(scalar: ImageExposureAdjust.Scalar) -> ImageExposureAdjust {
        ImageExposureAdjust(scalar: scalar)
    }
}

func applyFilters<each Filter: ImageFilter>(_ filter: repeat each Filter) {
    let _ = (repeat (each filter))
}

func test() {
    applyFilters(
        .blur(radius: 10), // ❌ Cannot convert value of type 'Int' to expected argument type 'ImageBlurFilter.Radius'
        .exposureAdjust(scalar: 0.5) // ❌ Cannot convert value of type 'Double' to expected argument type 'ImageExposureAdjust.Scalar'
    )

    applyFilters(
        .blur(radius: ImageBlurFilter.Radius(10)), // ✅
        .exposureAdjust(scalar: ImageExposureAdjust.Scalar(0.5)) // ✅
    )

    let blurRadius: ImageBlurFilter.Radius = 10 
    let exposureAdjustScalar: ImageExposureAdjust.Scalar = 0.5

    applyFilters(
        .blur(radius: blurRadius), // ✅
        .exposureAdjust(scalar: exposureAdjustScalar) // ✅
    )
}

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.