Can't use function type as generic type constraint in Swift

Gregory Higley

It's very possible I'm just doing it wrong. I'm trying to create a coalesce function in Swift, really just as an exercise in learning the language. I want to make it as generic as possible, so I want to be able to coalesce any SequenceType. Here's what I have, with overloads:

func coalesce(S: SequenceType, T: @autoclosure () -> Any? where S.Generator.Element == T>(values: S) -> Any? {
    for value: () -> Any? in values {
        if let found = value() {
            return found
        }
    }
    return nil
}

func coalesce(values: @autoclosure () -> Any?...) -> Any? {
    return coalesce(values)
}

The compiler tells me that T is not convertible to () -> Any? and further that it "expected a type name or protocol composition restricting T."

Now, I understand what this means. The compiler is telling me that function types are not eligible to be used as generic type constraints. If true, this is a real shame. One of the things I really love about Swift is that it does not commit the sin of having a bifurcated type system when it comes to nullability. But it looks like it does bifurcate the type system when it comes to function types. They are treated differently. And this means that writing highly general code around function types becomes very difficult if not impossible.

I could of course write this, but this is not general enough for my taste:

func coalesce(values: [@autoclosure () -> Any?]) -> Any?

Anyone know how to achieve something like this using SequenceType? I am considering filing a bug with Apple about this.

rintaro

In this case, typealias do the trick.

typealias AutoclosuredOptionalAny = @autoclosure () -> Any?

func coalesce<S: SequenceType where S.Generator.Element == AutoclosuredOptionalAny>(values: S) -> Any? {
    for value: AutoclosuredOptionalAny in values {
        if let found = value() {
            return found
        }
    }
    return nil
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't I use a Guid as a generic type constraint?

From Dev

Swift generic array with type constraint

From Dev

Swift generic array with type constraint

From Dev

Swift Cast to Generic Type with Constraint

From Dev

Why doesn't swift infer the appropriate overload function with a generic return argument without a type constraint?

From Dev

Generic type in generic constraint

From Dev

Generic type in generic constraint

From Dev

Why can't a protocol not be used as a type for a generic type in swift?

From Dev

What does this generic type constraint mean in Swift?

From Dev

Making a generic type constraint on Func<T>

From Dev

Swift: Can't use nested/chained generic type constraints (Policy pattern)

From Dev

In Swift, can I use function type in tuple?

From Dev

Apple Swift: Generic type constraint for String type only not works

From Dev

Swift function with generic argument type

From Dev

Type casting in a generic swift function

From Dev

Type casting in a generic swift function

From Dev

F# Type Constraint Mismatch when using a constraint on a generic function

From Dev

Generic type constraint and variance

From Dev

Generic type constraint not applying

From Dev

Generic constraint of the same type

From Dev

Declare function that takes generic type that conform to “can be multiplied” in Swift

From Dev

Why can't I return the generic type T from this function?

From Dev

Declare a function in Swift that returns a closure with a generic T.Type

From Dev

Can't serialize generic type

From Dev

Why can't I use covariance with two generic type parameters?

From Dev

Swift type does not conform to protocol error at generic constraint but not at class itself

From Dev

Swift type does not conform to protocol error at generic constraint but not at class itself

From Dev

Explicitly specify generic type constraint when calling function

From Dev

Explicitly specify generic type constraint when calling function

Related Related

  1. 1

    Why can't I use a Guid as a generic type constraint?

  2. 2

    Swift generic array with type constraint

  3. 3

    Swift generic array with type constraint

  4. 4

    Swift Cast to Generic Type with Constraint

  5. 5

    Why doesn't swift infer the appropriate overload function with a generic return argument without a type constraint?

  6. 6

    Generic type in generic constraint

  7. 7

    Generic type in generic constraint

  8. 8

    Why can't a protocol not be used as a type for a generic type in swift?

  9. 9

    What does this generic type constraint mean in Swift?

  10. 10

    Making a generic type constraint on Func<T>

  11. 11

    Swift: Can't use nested/chained generic type constraints (Policy pattern)

  12. 12

    In Swift, can I use function type in tuple?

  13. 13

    Apple Swift: Generic type constraint for String type only not works

  14. 14

    Swift function with generic argument type

  15. 15

    Type casting in a generic swift function

  16. 16

    Type casting in a generic swift function

  17. 17

    F# Type Constraint Mismatch when using a constraint on a generic function

  18. 18

    Generic type constraint and variance

  19. 19

    Generic type constraint not applying

  20. 20

    Generic constraint of the same type

  21. 21

    Declare function that takes generic type that conform to “can be multiplied” in Swift

  22. 22

    Why can't I return the generic type T from this function?

  23. 23

    Declare a function in Swift that returns a closure with a generic T.Type

  24. 24

    Can't serialize generic type

  25. 25

    Why can't I use covariance with two generic type parameters?

  26. 26

    Swift type does not conform to protocol error at generic constraint but not at class itself

  27. 27

    Swift type does not conform to protocol error at generic constraint but not at class itself

  28. 28

    Explicitly specify generic type constraint when calling function

  29. 29

    Explicitly specify generic type constraint when calling function

HotTag

Archive