【问题标题】:How do I reassign a function which takes a generic type argument in Swift?如何在 Swift 中重新分配一个接受泛型类型参数的函数?
【发布时间】:2020-01-02 03:53:17
【问题描述】:

披露,这是我写任何 Swift 的第一天,我来自 JS/TS 背景。

我习惯于简单地重新分配函数,如下所示:

let assertEqual = XCTAssertEqual

XCTAssertEqual 有以下声明:

func XCTAssertEqual<T>(_ expression1: @autoclosure () throws -> T, _ expression2: @autoclosure () throws -> T, accuracy: T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) where T : FloatingPoint

swift playground 抛出以下错误:

Generic parameter 'T' could not be inferred

我意识到这个赋值并不是特别“有价值”,但我将来可能想用泛型类型参数给其他函数起别名,并且想了解更多关于任何 Swift 特定约定的信息。

【问题讨论】:

    标签: swift function assignment-operator


    【解决方案1】:

    错误消息非常清楚地说明了您需要在此处做什么 - 告诉编译器 T 应该是什么类型。不幸的是,您不能拥有一个未绑定T 的“通用变量”。

    为此,您需要写出函数XCTAssertEquals&lt;T&gt;完整类型名称,对于T == Double,它是:

    (@autoclosure () throws -> Double, @autoclosure () throws -> Double, Double, @autoclosure () -> String, StaticString, UInt) -> ()
    

    所以你需要:

    let assertEqual: (@autoclosure () throws -> Double, @autoclosure () throws -> Double, Double, @autoclosure () -> String, StaticString, UInt) -> () = XCTAssertEqual
    

    我知道,这是一团糟。所以如果你要为各种Ts做这个,你可以先为长函数名做一个类型别名:

    typealias XCTAssertEqualsType<T> = (@autoclosure () throws -> T, @autoclosure () throws -> T, T, @autoclosure () -> String, StaticString, UInt) -> ()
    

    然后您可以使用XCTAssertEqualsType&lt;Double&gt;XCTAssertEqualsType&lt;Float&gt; 等。


    但老实说,我不明白你为什么要给这个断言函数起别名。作为一个函数,你失去了很多它的特性。如果您通过变量调用它,则必须手动传入文件名和行号“魔术”参数。你失去了所有可选参数,正如我一开始所说,你失去了泛型。

    如果你想要的只是函数的不同名称,也许你自己声明另一个函数:

    func anotherName<T>(_ expression1: @autoclosure () throws -> T, _ expression2: @autoclosure () throws -> T, accuracy: T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) where T : FloatingPoint {
        XCTAssertEqual(try expression1(), try expression2(), accuracy: accuracy, message(), file: file, line: line)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多