[英]Generic parameter 'T' could not be inferred / Cannot explicitly specialize a generic function
I have a function with this signature written in a Swift project in Xcode 7.3.1: 我有一个带有此签名的函数,该函数在Xcode 7.3.1的Swift项目中编写:
func DLog<T>(@autoclosure object: () -> T, _ file: String = #file, _ function: String = #function, _ line: Int = #line) {
}
The compiler complains about Generic parameter 'T' could not be inferred
for this call: 编译器抱怨
Generic parameter 'T' could not be inferred
为此调用Generic parameter 'T' could not be inferred
:
DLog({ var text = "Returning output list\n"; for outline in outlines { text = text + outline.debugDescription + "\n"; }; return text; })
When I try to provide the type it complains Cannot explicitly specialize a generic function
: 当我尝试提供类型时,它抱怨
Cannot explicitly specialize a generic function
:
DLog<String>({ var text = "Returning output list\n"; for outline in outlines { text = text + outline.debugDescription + "\n"; }; return text; })
I have tried few more approaches but nothing satisfied the compiler. 我尝试了几种其他方法,但编译器没有满意的结果。 I have also failed to find a hint how to solve this case.
我也没有找到如何解决这种情况的提示。
How to build the text inside the () => T
argument and pass it to the function properly? 如何在
() => T
参数中构建文本并将其正确传递给函数?
Compilation fails because of @autoclosure
attribute. 由于
@autoclosure
属性,编译失败。 When you pass some expression to function that takes @autoclosure
the compiler creates a closure with no parameters that returns result of that expression. 当您将某些表达式传递给采用
@autoclosure
的函数时,编译器将创建一个不带返回该表达式结果的参数的闭包。 So, when you pass { var text = "Returning output list\\n"; for outline in outlines { text = text + outline.debugDescription + "\\n"; }; return text; }
因此,当您传递
{ var text = "Returning output list\\n"; for outline in outlines { text = text + outline.debugDescription + "\\n"; }; return text; }
{ var text = "Returning output list\\n"; for outline in outlines { text = text + outline.debugDescription + "\\n"; }; return text; }
{ var text = "Returning output list\\n"; for outline in outlines { text = text + outline.debugDescription + "\\n"; }; return text; }
then compiler creates a closure returning a closure returning string. { var text = "Returning output list\\n"; for outline in outlines { text = text + outline.debugDescription + "\\n"; }; return text; }
然后编译器创建一个封闭返回关闭返回字符串。
To fix this, you can either add ()
to the end of expression: 要解决此问题,您可以在表达式的末尾添加
()
:
DLog({ () -> String in var text = "Returning output list\n"; for outline in outlines { text = text + outline.debugDescription + "\n"; }; return text; }())
or simplify expression to simple method call, eg 或将表达式简化为简单的方法调用,例如
DLog(outlines.reduce("Returning output list\n") { $0 + $1.debugDescription + "\n"; })
I don't know why it is causing it, but compiling it in swift 3 make this error message appear: 我不知道为什么会导致它,但是在swift 3中对其进行编译会使此错误消息出现:
Unable to infer complex closure return type;
无法推断复杂的闭包返回类型; add explicit type to disambiguate
添加显式类型以消除歧义
So I tried to explicitly add the return type for the closure: 所以我尝试显式添加闭包的返回类型:
DLog(
{ () -> String in
var text = "Returning output list\n"
for outline in outlines {
text = text + outline.debugDescription + "\n";
}
return text
}
)
and it worked. 而且有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.