假设我有这段代码:
func hello<T>(thing: T) -> String {
return "hello \(thing)"
}
Run Code Online (Sandbox Code Playgroud)
我是否可以编写一个hello
不能编译的函数版本,如果它是通过可选的?
let foo = "some"
let bar: String? = nil
print(helloNoOptional(foo)) // should compile
print(helloNoOptional(bar)) // should not compile
Run Code Online (Sandbox Code Playgroud)
我想也许它可以通过where
T上的协议一致性或条款来实现,但是我无法想到它究竟是如何工作的.
我想这样做的原因是因为我正在处理遗留代码库中的实际函数,如果thing
是nil 则没有明智的行为.因此,我宁愿防止hello
被调用于一个可选项,而不是处理thing
在hello中解包并尝试找出合理的错误行为.
更新:
一条可能的路径......我意识到Optional枚举符合NilLiteralConvertible
协议.因此,如果我能找到一种方法来限制我的泛型不符合类型,我可以事实上排除选项.但我不知道是否有可能做类似的事情
<T where !T: NilLiteralConvertible>
Run Code Online (Sandbox Code Playgroud)
我能想到的最好是在运行时重载和检查:
func hello<T>(thing: T) -> String {
return "hello \(thing)"
}
fun hello<T>(thing: T?) -> String {
fatalError("No optionals allowed!")
}
hello("swift") // fine
hello(2) // fine
hello(Int("2")) // fatal error
Run Code Online (Sandbox Code Playgroud)
但我不知道一种生成编译时错误的方法.
归档时间: |
|
查看次数: |
972 次 |
最近记录: |