如何在Swift中将类型方法添加到泛型类型中? [英] How to add type methods to generic types in Swift?

查看:210
本文介绍了如何在Swift中将类型方法添加到泛型类型中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Swift中为泛型类添加一个类型(aka static)方法。这是代码。

  class StateArchive< T> {
class func emptyAllArchives(){
//做某事
}
}

//编译器发出一个错误:通用参数的参数'T'无法推断
StateArchive.emptyAllArchives()

我可以得到上面的代码通过提供类型代替T来编译,如下所示:


$ b

StateArchive< AnyObject> .emptyAllArchives()



然而,这看起来很尴尬。最初,我认为这样做的原因可能是可以创建一个类型为T的类变量。操场上的快速测试显示编译器发出消息: >类存储的属性尚未支持泛型类型。



有谁知道如何在泛型中创建类型方法,并让客户端调用此方法而不提供类型代替T?

解决方案

当遇到泛型问题时,以帮助理解问题,思考如果用不同的具体类型替换占位符会发生什么。

例如,假设您定义了以下内容(这赢得了'因为你的理由,你可以编译它,但想象一下):

  class C< T> {
static var a:[T] = []

static func addElement(i:T){
a.append(i)
}
}

真的,当您编写 class C< T> ,你不是在写课程。您正在编写无限数量的可能类的蓝图 - C< Int> C< String> C



在这种情况下,假设你写了 C.addElement 1) - 所以 T 会是 Int 。然后,你写了 C.addElement(one)。现在 C.a 是否包含一个或两个项目? a 的类型是什么?



可能最合理的答案是会有一个 C< Int> .a 和一个元素 C< String> .a 。但是这可能会让人感到困惑,并且似乎没有用例来证明这种功能。

通常情况下,静态方法和属性很少被使用,你可能会发现你最好使用非静态成员和方法,结合单例模式(或者可能甚至没有)。如果您仍然需要类似静态的函数,那么使用通用的自由函数可能会更好(例如 func emptyArchive< T:C>(c:C){} )。


I am trying to add a type (aka static) method to a generic class in Swift. Here is the code.

class StateArchive<T> {
    class func emptyAllArchives() {
        // do something
    }
}

// the compiler emits an error: "Argument for generic parameter 'T' could not be inferred"
StateArchive.emptyAllArchives()

I can get the above code to compile by providing the type in place of T like so:

StateArchive<AnyObject>.emptyAllArchives()

This looks awkward, however. Initially I though that the reason for this could be that one could create a class variable with type T. A quick test in the playground revealed that the compiler emits a message saying:

"Class stored properties not yet supported in generic types".

Does anyone know how to create a type method in a generic type and let the client call this method without providing the type in place of T?

解决方案

When encountering an issue with generics that doesn’t work the way expect it’s often useful, to help understand the issue, to think about what would happen if you replaced a placeholder with different concrete types.

So for example, suppose you defined the following (this won’t compile for the reason you give, but imagine it did):

class C<T> {
    static var a: [T] = []

    static func addElement(i: T) {
        a.append(i)
    }
}

Really, when you write class C<T>, you aren’t writing a class. You’re writing a blueprint for an infinite number of possible classes – C<Int>, C<String>, C<AnythingElse>.

Under these circumstances, suppose you wrote C.addElement(1) – so T would be Int. Then later, you wrote C.addElement("one"). Would C.a now contain one item, or two? What would the type of a be?

Probably the most reasonable answer would be that there would be a C<Int>.a with one element, and a C<String>.a with one element. But this could get very confusing, and it doesn’t seem like there are use cases to justify this kind of functionality.

Generally, static methods and properties are best used rarely, and you may find you are better off with non-static members and methods, combined with a singleton pattern (or maybe not even that). If you still find the need for static-like functions, you may be better off using a generic free function (e.g. func emptyArchive<T: C>(c: C) { }).

这篇关于如何在Swift中将类型方法添加到泛型类型中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆