【问题标题】:Generic typealias in SwiftSwift 中的通用类型别名
【发布时间】:2014-11-23 01:26:48
【问题描述】:

在 haskell 中你可以这样做:

type Parser a = String -> [(a, String)]

我尝试在 Swift 中制作类似的东西。到目前为止,我没有运气编写这些代码。

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

那么这在 swift 中是不可能的吗?如果是,还有其他方法可以实现这种行为吗?

更新: swift 3 现在似乎支持通用类型别名 https://github.com/apple/swift/blob/master/CHANGELOG.md

【问题讨论】:

    标签: swift generics


    【解决方案1】:

    通用 typealias 从 Swift 3.0 开始可以使用。这应该适合你:

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

    这里是完整的文档:https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/typealias-declaration

    用法(来自@Calin Drule 评论):

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

    【讨论】:

    【解决方案2】:

    typealias 目前不能与泛型一起使用。您最好的选择可能是将解析器函数包装在结构中。

    struct Parser<A> {
        let f: String -> [(A, String)]
    }
    

    然后您可以在创建解析器时使用尾随闭包语法,例如

    let parser = Parser<Character> { string in return [head(string), tail(string)] }
    

    【讨论】:

    • @mustafa 嗨,你能告诉我它的哪一部分表示拳击吗?您不是指 Java 意义上的拳击,是吗?另外,我在处理[head(string)...tail(string)] 时遇到了问题。谢谢。
    • @Unheilig Boxing 是指将一种类型包装在另一种类型周围,以便利用仅在外部类型中发现的功能。您可能熟悉 Objective-C 中的概念,其中像整数这样的基元不能在没有被“装箱”在 NSNumber 中的情况下传递到 NSArray,例如,@(1234)。头/尾不是特定的,而是将数组拆分为第一项(头)和其余项(尾)的常见模式。见chris.eidhof.nl/posts/swift-tricks.html
    • @clozach 感谢您的解释。我会继续浏览您分享的链接。
    • 这个答案不是最新的。你应该检查stackoverflow.com/a/39779643/590864
    【解决方案3】:

    通用类型别名 - SE-0048

    状态:已实现 (Swift 3)

    解决方案很简单:允许类型别名引入类型参数,这些参数在其定义范围内。这允许人们表达以下内容:

    typealias StringDictionary<T> = Dictionary<String, T>
    typealias IntFunction<T> = (T) -> Int
    typealias MatchingTriple<T> = (T, T, T)
    alias BackwardTriple<T1, T2, T3> = (T3, T2, T1)
    

    【讨论】:

      【解决方案4】:

      这里我将展示 typealias 的示例,向您展示如何在协议定义中使用 typealias:我希望这有助于您理解 typealias

      protocol NumaricType {
          typealias elementType
          func plus(lhs : elementType, _ rhs : elementType) -> elementType
          func minus(lhs : elementType, _ rhs : elementType) -> elementType
      }
      
      struct Arthamatic :NumaricType {
      
          func addMethod(element1 :Int, element2 :Int) -> Int {
             return plus(element1, element2)
          }
          func minusMethod(ele1 :Int, ele2 :Int) -> Int {
              return minus(ele1, ele2)
          }
      
          typealias elementType = Int
      
          func plus(lhs: elementType,  _ rhs: elementType) -> elementType {
              return lhs + rhs
          }
          func minus(lhs: elementType, _ rhs: elementType) -> elementType {
              return lhs - rhs
          }
      }
      

      输出:

      let obj =  Arthamatic().addMethod(34, element2: 45) // 79
      

      【讨论】:

      • 嘿@Narendra G 你觉得这个解决方案也适用于其他数字类型吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      相关资源
      最近更新 更多