如何重新分配在Swift中接受泛型类型参数的函数?

如何重新分配在Swift中接受泛型类型参数的函数?,swift,function,assignment-operator,Swift,Function,Assignment Operator,披露,这是我写Swift的第一天,我来自JS/TS背景 我习惯于简单地重新分配函数,如下所示: 让assertEqual=xctasertequal XCTASERTEQUAL有以下声明: func-XCTAssertEqual(expression1:@autoclosure()throws->T,expression2:@autoclosure()throws->T,精度:T,u消息:@autoclosure()->String=”“,file:StaticString=#file,lin

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

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

让assertEqual=xctasertequal
XCTASERTEQUAL有以下声明:

func-XCTAssertEqual(expression1:@autoclosure()throws->T,expression2:@autoclosure()throws->T,精度:T,u消息:@autoclosure()->String=”“,file:StaticString=#file,line:UInt=#line)其中T:FloatingPoint
swift游乐场抛出以下错误:

Generic parameter 'T' could not be inferred

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

错误消息非常清楚地说明了您需要在这里做什么-告诉编译器
t
应该是什么类型。不幸的是,您不能拥有具有未绑定的
t
的“泛型变量”

为此,您需要写出函数的完整类型名
xctasertequals
,对于
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
s执行此操作,可以首先对长函数名执行类型别名:

typealias XCTAssertEqualsType<T> = (@autoclosure () throws -> T, @autoclosure () throws -> T, T, @autoclosure () -> String, StaticString, UInt) -> ()
typealias XctaserteQualsType=(@autoclosure()抛出->T,@autoclosure()抛出->T,T,@autoclosure()->字符串,静态字符串,UInt)->()
然后可以使用
xctasertequaltype
xctasertequaltype


但老实说,我不明白为什么要将这个断言函数别名。作为一个函数,它会丢失很多特性。如果通过变量调用,则必须手动传入文件名和行号“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)
}
func另一个名称(expression1:@autoclosure()throws->T,expression2:@autoclosure()throws->T,精度:T,message:@autoclosure()->String=“”,file:StaticString=#file,line:UInt=#line),其中T:FloatingPoint{
xctasertequal(try expression1(),try expression2(),精度:精度,消息(),文件:文件,行:行)
}

错误信息非常清楚您需要在此处执行什么操作-告诉编译器
T
应该是什么类型。不幸的是,您不能拥有具有未绑定的
t
的“泛型变量”

为此,您需要写出函数的完整类型名
xctasertequals
,对于
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
s执行此操作,可以首先对长函数名执行类型别名:

typealias XCTAssertEqualsType<T> = (@autoclosure () throws -> T, @autoclosure () throws -> T, T, @autoclosure () -> String, StaticString, UInt) -> ()
typealias XctaserteQualsType=(@autoclosure()抛出->T,@autoclosure()抛出->T,T,@autoclosure()->字符串,静态字符串,UInt)->()
然后可以使用
xctasertequaltype
xctasertequaltype


但老实说,我不明白为什么要将这个断言函数别名。作为一个函数,它会丢失很多特性。如果通过变量调用,则必须手动传入文件名和行号“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)
}
func另一个名称(expression1:@autoclosure()throws->T,expression2:@autoclosure()throws->T,精度:T,message:@autoclosure()->String=“”,file:StaticString=#file,line:UInt=#line),其中T:FloatingPoint{
xctasertequal(try expression1(),try expression2(),精度:精度,消息(),文件:文件,行:行)
}