我可以将通用参数约束为*not*是可选的吗?

rog*_*err 13 generics swift

假设我有这段代码:

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)

我想也许它可以通过whereT上的协议一致性或条款来实现,但是我无法想到它究竟是如何工作的.

我想这样做的原因是因为我正在处理遗留代码库中的实际函数,如果thing是nil 则没有明智的行为.因此,我宁愿防止hello被调用于一个可选项,而不是处理thing在hello中解包并尝试找出合理的错误行为.

更新:

一条可能的路径......我意识到Optional枚举符合NilLiteralConvertible协议.因此,如果我能找到一种方法来限制我的泛型符合类型,我可以事实上排除选项.但我不知道是否有可能做类似的事情

<T where !T: NilLiteralConvertible>
Run Code Online (Sandbox Code Playgroud)

Air*_*ity 5

我能想到的最好是在运行时重载和检查:

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)

但我不知道一种生成编译时错误的方法.