一个通用的值是一个集合

huangapple go评论44阅读模式
英文:

A generic which is value is a collection

问题

我希望能够创建一个具有通用类型的类,并创建一个扩展,用于处理通用类型为Dictionary的情况。

例如:

public final class AtomicCollection<WrappedValue> {

}

我想要能够实现所有Dictionary/Array/Set具有的方法,例如 func removeAll()

我尝试使用扩展来实现:

extension AtomicCollection where WrappedValue == Dictionary {

}

问题是,我收到以下错误:

Reference to generic type 'Dictionary' requires arguments in <...>

这将导致我需要提及Dictionary的Key和Value类型,Element是Array的类型等。

我希望能够为所有的Dictionary、Array和Set创建通用约束。

有什么想法?

英文:

I wish to be able to create a class with a generic type, and make an extension for the use case in which the generic is a Dictionary.

E.G:

public final class AtomicCollection&lt;WrappedValue&gt; {

}

I want to be able to implement all the methods a Dictionary/Array/Set has, for example func removeAll().

I tried to do that with an extension so:

extension AtomicCollection where WrappedValue == Dictionary {

}

The problem is, I get the following error:

Reference to generic type &#39;Dictionary&#39; requires arguments in &lt;...&gt;

This will result in the fact that I need to mention the type of which the Key and the Value are fora Dictionary, Element is for an Array etc..

I want to be able to create a generic constraint for ALL Dictionaries, Arrays and Sets.

Any idea?

答案1

得分: 1

extension AtomicCollection where WrappedValue: Collection {}

如果你需要能够调用 removeAll(),那么协议是 RangeReplaceableCollection(这也意味着符合 Collection 协议):

extension AtomicCollection where WrappedValue: RangeReplaceableCollection {}

你只需要选择(或创建)一个具有你所需方法的协议来应用于这组算法。这正是协议的整个目的:定义一组算法可以操作的方法。

与类似集合的协议相关的协议有相当多。你可以在 Sequence and Collection Protocols 中找到它们的概述。

英文:

The key thing is you're not asking for Dictionary. Your title captures your intent: all collections. So that's what you want:

extension AtomicCollection where WrappedValue: Collection {}

If you need to be able to call removeAll(), the protocol is RangeReplaceableCollection (which also implies Collection):

extension AtomicCollection where WrappedValue: RangeReplaceableCollection {}

You just need to pick (or create) a protocol that has the methods you need for this group of algorithms. That's the whole point of protocols: to define a set of methods that algorithms can operate on.

There are quite a few protocols related to collection-like things. You can find an overview of them in Sequence and Collection Protocols.

答案2

得分: 1

目前您可以通过为扩展中的每个函数指定通用条件来实现此目的,但不能针对整个扩展本身实现此目的:

extension AtomicCollection {
    func removeAll<T1, T2>() where WrappedValue == Dictionary<T1, T2> {
    }
}
英文:

Currently you can do this by specifying generic conditions for each function in the extension, but not for the whole extension itself:

extension AtomicCollection {
    func removeAll&lt;T1, T2&gt;() where WrappedValue == Dictionary&lt;T1, T2&gt; {
    }
}

答案3

得分: -1

这次讨论导致我创建了自己的协议,以暴露特定功能,就像@rob-napier所提供的一样。

这导致了一个简单而相当天真的解决方案,基于GCD,在Swift中适用于所有类型,包括集合。

你可以在这里查看!

https://gitlab.com/yanivhasbanidev/atomics

感谢大家对以上的帮助!

英文:

This discussion resulted in me creating my own protocols to expose specific functionalities, as offered by @rob-napier.

This resulted in a simple and quite naive solution, based on GCD, for Atomicity in Swift for every type, including collection.

You can check it out here!

https://gitlab.com/yanivhasbanidev/atomics

Thanks everyone for assisting with the above!

huangapple
  • 本文由 发表于 2023年3月31日 03:06:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定