为什么我可以用泛型快速制定相同类型的要求?有什么办法吗? [英] Why can i make same-type requirement in swift with generics? Is there any way?
问题描述
public final class Process< InputType,OutputType,Memory>
我想让该函数仅适用于InputType和
OutputType恰好相同的情况相同的类型。
所以我尝试像这样:
扩展程序其中InputType == OutputType {}
但这会导致:
相同类型的需求使通用参数
InputType
和
OutputType
等价
然后,我走了很远,试图这样做:
func bypass< SameType>() - >处理< SameType,SameType,Memory> OutputType == InputType {}
但是这会导致完全相同的错误。
所以问题是为什么我不能以这种方式定义泛型,以至于两个泛型类型是等价的,因为这正是我想要的。我想定义只适用于这种情况的函数,如果不遵循这个规则,那么编译时就会失败。
public static func bypass< SameType>() - >处理< SameType,SameType,Memory>
这最终只会在运行时失败,甚至不会在创建时失败,但是当具体类触发了操作时。
有什么方法可以定义扩展
或函数
相同类型的泛型参数会导致编译时错误?
更新:实现的一些细节被遗漏导致代码不可读,对于上下文无关紧要在 Swift 4及更高版本中,您可以编写
p> public final class Process< InputType,OutputType,Memory> {
// ...
}
扩展程序其中InputType == OutputType {
func bypass() - >处理<输入类型,输出类型,存储器> {
// ...
}
}
原始答案(Swift 3): 尽管一些更改即将到来Swift 4.然而,你可以在协议上约束类型。你可以制定一个只有 Ok so i have some class defined like this: And i want to make the function available only for case when InputType and
OutputType are exactly same type.
So i tried like this like this: But this would result in: Same-type requirement makes generic parameters So then i've gone a bit far and tried to do it like this: But this would result in exactly same error.
So the question is why can't i define generics in such way that two generic types would be Equivalent, cause that's exactly what i wanted. I wanted to define function available only for this case, that would fail at compile time if this rule is not followed. So right now i'm using something like this: Which would ultimately fail only at runtime and not even when created but when the concrete class is triggered for action. Is there any way to define Update: some details of implementations are missed cause would make code unreadable and they are not critical for the context In Swift 4 and later, you can write:
Original answer (Swift 3): You can't constraint types on generic classes yet even though some changes are coming in Swift 4. However, you can constraint types on a protocol. You can make a protocol that only
这篇关于为什么我可以用泛型快速制定相同类型的要求?有什么办法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
Process
符合的协议:
protocol ProcessProtocol {
//我还没有找到一种方法将这些关联类型命名为
// //类中的那些类型。如果有人发现某种方式,请告诉我们
associatedtype IT
associatedtype OT
associatedtype MT
}
final public class Process< InputType,OutputType, MemoryType>:ProcessProtocol {
typealias IT = InputType
typealias OT = OutputType
typealias MT = MemoryType
//您的代码
}
//注意这是协议的扩展,而不是类
扩展ProcessProtocol其中IT == OT {
func foo(){
//这个函数是唯一的当InputType = OutputType
}
}
时可用public final class Process<InputType, OutputType, Memory>
extension Process where InputType == OutputType { }
InputType
and
OutputType
equivalentfunc bypass<SameType>() -> Process<SameType, SameType, Memory> where OutputType == InputType {}
public static func bypass<SameType>() -> Process<SameType, SameType, Memory>
extension
or function
for generic parameters of same type that would just not compile(result in compile time error)?public final class Process<InputType, OutputType, Memory> {
// ...
}
extension Process where InputType == OutputType {
func bypass() -> Process<InputType, OutputType, Memory> {
// ...
}
}
Process
conforms to like this:protocol ProcessProtocol {
// I haven't found a way to name these associated type identically to
// those in the class. If anyone discover a way, please let me know
associatedtype IT
associatedtype OT
associatedtype MT
}
final public class Process<InputType, OutputType, MemoryType>: ProcessProtocol {
typealias IT = InputType
typealias OT = OutputType
typealias MT = MemoryType
// your code
}
// Note that this is an extension on the protocol, not the class
extension ProcessProtocol where IT == OT {
func foo() {
// this function is only available when InputType = OutputType
}
}