0

I am using Xcode 9.2.

I am receiving the error:

Cannot convert value of type '(ValueType2) -> Void' to expected argument type '(_) -> Void'

I have the following code:

import UIKit

enum Value<ValueType> {

    case hasValue(ValueType)

}

class BaseClass<ValueType> {

    var value: Value<ValueType>?

    func f1<ValueType2>(closure: @escaping (ValueType, @escaping (ValueType2) -> Void) -> Void ){

        let s1 = SubClass<ValueType2>()

        // The point of the generic type conversion issue
        // Cannot convert value of type '(ValueType2) -> Void' to expected argument type '(_) -> Void'

        closure(value!, s1.f2)

    }

}

class SubClass<ValueType>: BaseClass<ValueType> {

    func f2(value: ValueType) -> Void { /* DO SOMETHING */ }

}

I am unsure whether this is related to the Swift bug being fixed in Swift 4.1:

Swift Generics: Cannot convert value of type to expected argument type

As autocomplete knows the class's correct generic type parameter & function's parameter - ValueType2 - in both cases.

Any ideas on how to overcome this error?

Thanks

Mark Jarecki
  • 115
  • 1
  • 13
  • It looks like it might be an instance of that bug. If I replace all instances of `ValueType2` with a concrete type like `Int` the error goes away. – JeremyP Mar 08 '18 at 10:54
  • Just what I thought. – Mark Jarecki Mar 08 '18 at 11:07
  • I also noticed that if I remove ValueType generic parameter from the f1(closure:) method, the compiler issue goes away. It seems the compiler does not like two generic types in a closure. – Mark Jarecki Mar 08 '18 at 11:36

1 Answers1

2

In your f2, the type of closure is @escaping (ValueType, @escaping (ValueType2) -> Void) -> Void.

The type of the first parameter is ValueType. But you are using it as:

closure(value!, s1.f2)

The type of the first argument value! is Value<ValueType>, not ValueType.


If you change the type of closure to @escaping (Value<ValueType>, @escaping (ValueType2) -> Void) -> Void, your code would compile without any issue.

You may have found some issue about generics, but at least, your code example is not describing the issue.

OOPer
  • 47,149
  • 6
  • 107
  • 142