Generic typealias in Swift

generics, swift

Solution

Generic `typealias` can be used since Swift 3.0. This should work for you:

typealias Parser<A> = (String) -> [(A, String)]

Here is the full documentation: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/typealias-declaration

Usage (from @Calin Drule comment):

func parse<A>(stringToParse: String, parser: Parser) 

Problem

In haskell you can do this: ``` type Parser a = String -> [(a, String)] ``` I tried to make something similar in Swift. So far I wrote these codes with no luck. ``` typealias Parser<A> = String -> [(A, String)] typealias Parser a = String -> [(a, String)] typealias Parser = String -> [(A, String)] ``` So is this simply impossible in swift? And if it is is there another ways to implement this behavior? UPDATE: It seems generic typealiases are now supported in swift 3 https://github.com/apple/swift/blob/master/CHANGELOG.md

Original source