我如何在Swift中重新分配一个采用泛型类型参数的函数?

问题描述 投票:0回答:1

公开,这是我写任何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

快速操场抛出以下错误:

Generic parameter 'T' could not be inferred

[我意识到这种分配不是特别“有价值”,但是将来我可能想用泛型类型参数为其他函数添加别名,并且想了解有关Swift特定约定的更多信息。

swift function assignment-operator
1个回答
0
投票

该错误消息非常清楚您在这里需要执行的操作-告诉编译器T应该是什么类型。不幸的是,您不能有一个未绑定T的“通用变量”。

为此,您需要写出函数XCTAssertEquals<T>完整类型名称,对于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

我知道,那是一团糟。因此,如果要针对各种T执行此操作,则可以首先为长函数名创建类型别名:

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

然后您可以使用XCTAssertEqualsType<Double>XCTAssertEqualsType<Float>等。>


但是,老实说,我不明白为什么要为此断言函数起别名。您会失去很多功能。如果通过变量调用文件名和行号“ magic”参数,则必须手动输入该参数。您丢失了所有可选参数,并且正如我在开始时所说的,您丢失了泛型。

如果您想要的只是函数的其他名称,也许您自己声明另一个函数:

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)
}
© www.soinside.com 2019 - 2024. All rights reserved.