简体   繁体   English

无法使用类型为(T)的参数列表调用“追加”

[英]cannot invoke 'append' with an argument list of type '(T)'

Just got this strange error when trying to write a stack with generic type in latest playground. 尝试在最新的Playground中编写具有泛型类型的堆栈时遇到了这个奇怪的错误。 I really don't understand what's wrong here, can someone explain to me why I got this error? 我真的不明白这里出了什么问题,有人可以向我解释为什么我收到此错误吗?

class MyStack<T> {
    var stack1 = [T]()

    func push<T>(value: T) {
        stack1.append(value)  // Error: cannot invoke 'append' with an argument list of type '(T)'
    }
}

The class is already generic, no need to make push generic too 该类已经是通用的,也无需使push通用

class MyStack<T> {
    var stack1 = [T]()

    func push(value: T) {
        stack1.append(value)
    }
}

When push is declared as push<T> , the generic parameter overrides the one defined on the class. push声明为push<T> ,泛型参数将覆盖在类上定义的参数。 So, if we were to rename the generic parameters, we'd get 因此,如果我们要重命名通用参数,我们将得到

class MyStack<T1> {
    var stack1 = [T1]()

    func push<T2>(value: T2) {
        stack1.append(value)  // Error: cannot invoke 'append' with an argument list of type '(T2)'
    }
}

presented like this, it makes sense that we cannot push a T2 in [T1] . 像这样呈现,我们不能在[T1]T2是有意义的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用类型为“([TT])”的参数列表调用“append” - Cannot invoke 'append' with an argument list of type '([T.T])' Swift数组:无法使用类型&#39;(Int)&#39;的参数列表调用&#39;append&#39; - Swift Array: Cannot invoke 'append' with an argument list of type '(Int)' 无法为 type: 调用初始化程序,参数列表类型为: - Cannot invoke initializer for type: with an argument list of type: Swift 4无法使用类型为实参的列表调用&#39;index&#39; - Swift 4 Cannot invoke 'index' with an argument list of type 无法使用类型为((of:Any)&#39;的参数列表调用索引 - Cannot invoke index with an argument list of type '(of: Any)' 无法使用类型为&#39;(inout inout $ T6,inout inout $ T11)的参数列表调用&#39;subscript&#39; - Cannot invoke 'subscript' with an argument list of type '(inout inout $T6, inout inout$T11) 无法为类型为&#39;(CustomObject)&#39;的参数列表的类型为&#39;String&#39;的初始化程序 - Cannot invoke initializer for type 'String' with an argument list of type '(CustomObject)' 无法使用类型为&#39;([[(ModelMessageBridge)],atindex:int)&#39;的参数列表调用&#39;insert&#39; - Cannot invoke 'insert' with an argument list of type '([(ModelMessageBridge)], atindex: int)' 在Swift xcode 6.3.1中无法使用类型为&#39;([[(nameOfClass)])&#39;的参数列表调用&#39;functionName&#39; - Cannot invoke 'functionName' with an argument list of type '([(nameOfClass)])' in Swift xcode 6.3.1 Swift:2个类似的错误:无法使用类型为(([[xx])-&gt;())&#39;的参数列表调用&#39;x&#39; - Swift: 2 similar errors: Cannot invoke 'x' with an argument list of type '(([xx]) -> ())'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM