6

I would like to create a generic helper function that does some processing. One of its inputs will be a function returning an array of something.

I can't figure out how to do this. I keep getting a compile error. I found the post

argument for generic parameter could not be inferred

and tried adding ...as [String] or ... as [Int], etc. but no luck.

func helperThatDoesSomeGenericProcessing<T>(displayedStrings: () -> [T])  -> [String]! {
    let listOfSomething: [T] = displayedStrings()
    // do something with listOfSomething
    return ["some", "resulting", "string", "from", "the", "input"]
}

func concreteFunction1() -> [AnyObject]! {
    var s: [String] = helperThatDoesSomeGenericProcessing {  // ERROR: Argument for generic parameter 'T' could not be inferred.
        var str = ["One", "Two", "Three"]
    } // tried 'as [String]' here

    // do something with s
    return s
}

func concreteFunction2() -> [AnyObject]! {
    var s: [Int] = helperThatDoesSomeGenericProcessing {  // ERROR: Argument for generic parameter 'T' could not be inferred.
        var i = [1, 2, 3]
    } // tried 'as [Int]' here

    // do something with s
    return s
}
Community
  • 1
  • 1
Daniel
  • 3,758
  • 3
  • 22
  • 43
  • What are you trying to do exactly? This is pretty confusing without more detail about what exactly sort of processing you're trying to do... because it seems a bit like an XY problem maybe. – nhgrif Jul 05 '15 at 14:28

2 Answers2

5

Adding return appropriately and explicitly declaring the concrete type of () -> [T] solve the errors ... but I'm not sure it will get you what you want. Anyway here's the code:

func helperThatDoesSomeGenericProcessing<T>(displayedStrings: () -> [T])  -> [String]! {
    let listOfSomething: [T] = displayedStrings()
    // do something with listOfSomething
    return ["some", "resulting", "string", "from", "the", "input"]
}

func concreteFunction1() -> [AnyObject]! {
    var s: [String]! = helperThatDoesSomeGenericProcessing {
        () -> [String] in // Explicit declared type
        var str = ["One", "Two", "Three"]
        return str
    } // tried 'as [String]' here

    // do something with s
    return s
}

func concreteFunction2() -> [AnyObject]! {
    var s: [String]! = helperThatDoesSomeGenericProcessing {
        () -> [Int] in // Explicit declared type
        var i = [1, 2, 3]
        return i
    } // tried 'as [Int]' here

    // do something with s
    return s
}

Notice I also corrected the type of var s since your generic function always returns an implicitly unwrapped optional array of strings [String]!. The return type is not generalised (i.e.: T or [T] etc.).

Probably you might need to change some type declarations in order to achieve your design needs.

Hope this helps

Matteo Piombo
  • 6,688
  • 2
  • 25
  • 25
0

hi your closure body does no return

func concreteFunction1() -> [AnyObject]! {
    let s: [String] = helperThatDoesSomeGenericProcessing {  // ERROR: Argument for generic parameter 'T' could not be inferred.
    return ["One", "Two", "Three"]
} // tried 'as [String]' here

    // do something with s
    return s
}
beryllium
  • 29,669
  • 15
  • 106
  • 125
Christian Dietrich
  • 11,778
  • 4
  • 24
  • 32