- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我通常会对一种或另一种编程范式的优点充满诗意,但今天我决定直接回答我的问题。所以我开始了……
我有 4-5 个类似的结构。在每个字段中,前 3 个字段几乎相同。我将这些结构存储在一个数组中。由于每个结构都是其自己的类型,并且由于我所听到的都是通用的面向协议(protocol)的编程,因此我决定让它们都符合一个协议(protocol)。然后我可以轻松地将它们保存在同一个数组中,如下所示:vartables: [DataHoldable]
。
所以,我知道我需要创建 5 个结构体,所有这些结构体必须共享三个成员:format
、length
和 语言
。这是我所讨论内容的简短示例:
// Imagine `TableA`, `TableB`, etc.
struct Table[A-D] : DataHoldable {
var format: UInt16
var length: UInt16
var language: UInt16
...
}
struct TableE : DataHoldable {
var format: Int32
var length: UInt32
var language: UInt32
...
}
如您所见,TableE
的所有字段都具有更宽的整数,并且其 format
字段经过签名以达到最佳效果。
所以,这就是我认为方便的typealias
功能可以发挥作用的地方。
我做了一个像这样的协议(protocol):
protocol DataHoldable {
typealias FormatType = UInt16
typealias LengthType = UInt16
typealias LangType = UInt16
var format: FormatType { get }
var length: LengthType { get }
var language: LangType { get }
}
我认为,通过将默认类型 UInt16
分配给协议(protocol)的关联类型,我就不必在前 4 个结构中显式声明 UInt16
,从而允许我绕过大量的重新输入(双关语)。我想可以做这样的事情:
struct Table[A-D] : DataHoldable {
var format: FormatType
var length: LengthType
var language: LangType
...
}
struct TableE : DataHoldable {
typealias FormatType = Int32
typealias LengthType = UInt32
typealias LangType = UInt32
var format: FormatType
var length: LengthType
var language: LangType
...
}
但是,情况似乎并非如此,因为无论我做什么,我都开始收到一个又一个错误。我什至创建了协议(protocol)的扩展,因为我认为人们能够在扩展中使用默认的关联类型。然而,在这样做时,我引入了新的更可怕的错误。
我尝试了大约 10 种不同的组合和/或黑客和/或诡计,但似乎没有任何效果。
所以我的问题是:如果不能真正使代码通用,那么能够“通用”代码有什么意义呢?我错过了什么吗?任何帮助、想法、想法、可能的路线等将不胜感激。谢谢!
最佳答案
正如我在评论中所写,请参阅this relevant Q & A关于尝试在协议(protocol)中为关联类型(此处为typealias
)使用默认分配。
<小时/>Quote from user fqdn:s answer
"... when conforming to a protocol, you cannot directly refer to an associated type that was declared only within that protocol
(note, however, that when extending a protocol the associated type is available, as you would expect) "
现在,尝试使用默认(分配的)关联类型并不是真正的“协议(protocol)的通用强度”;相反(作为示例,在本例中),可以在协议(protocol)扩展中定义的方法中使用关联类型来实现默认协议(protocol)实现。
protocol DataHoldable {
typealias FormatType
typealias LengthType
typealias LangType
var format: FormatType { get }
var length: LengthType { get }
var language: LangType { get }
}
extension DataHoldable {
func getFormatAndLengthAsTuple() -> (FormatType, LengthType) {
return(format, length)
}
}
任何符合 DataHoldable
协议(protocol)的类型都可以访问默认方法 getFormatAndLengthAsTuple()
。此外,类型别名(关联类型)FormatType
、LengthType
和 LangType
的实际类型不需要在类型一致性中显式声明 DataHoldable
,可以通过属性format
、length
和language
的类型推断 .
struct TableA : DataHoldable {
var format: UInt16 // FormatType inferred to be UInt16
var length: UInt16 // LengthType inferred to be UInt16
var language: UInt16 // LangType inferred to be UInt16
init() {
format = 1
length = 2
language = 3
}
// ...
}
struct TableE : DataHoldable {
var format: Int32 // FormatType inferred to be UInt16
var length: UInt32 // FormatType inferred to be UInt16
var language: UInt32 // FormatType inferred to be UInt16
init() {
format = 4
length = 5
language = 6
}
// ...
}
这里存在协议(protocol)和协议(protocol)扩展的一些“通用强度”
let fooA = TableA()
let fooE = TableE()
print(fooA.getFormatAndLengthAsTuple()) // (1, 2)
print(fooE.getFormatAndLengthAsTuple()) // (4, 5)
<小时/>
最后,请注意,在协议(protocol)蓝图中使用 typealias T = SomeActualType
是非常不寻常的;更常见的方法是在关联类型上包含一些类型约束,typealias T: SomeTypeConstraint
。
例如,在您的情况下,将关联类型限制为 IntegerType
可能是合适的,允许您例如使用这些关联类型作为字典中的键,因为编译器知道所有符合 IntegerType
的类型也符合 Equatable
和 Hashable
)。
protocol DataHoldable {
typealias FormatType: IntegerType
// ...
var format: FormatType { get }
var dict : [FormatType:String] { get set }
}
extension DataHoldable {
mutating func setFormatKeyFooValue() {
dict[format] = "foo"
}
}
struct TableA : DataHoldable {
var format: UInt16 = 42
var dict : [UInt16:String] = [:]
// ...
}
var fooA = TableA()
fooA.setFormatKeyFooValue()
print(fooA.dict) // [42: "foo"]
关于swift - Swift 协议(protocol)类型别名中的默认类型可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35876268/
我收到此错误消息: .rvm/gems/ruby-2.5.1/bin/ruby_executable_hooks:24:in `' 我重新安装了 Ruby rvm reinstall ruby-2.5
我开始从事 WPF Ribbon 开发,非常好! 我的问题是找到(免费)基本图标(如文件保存/打开/等,剪切/粘贴/等)。 你有什么建议吗? 最佳答案 你看过Visual Studio Icon Li
我只找到经典的声音ID,但我需要Chord(默认)日历警报。如何播放声音? ks #define systemSoundID 1315 AudioServicesPlaySystemSound (s
在 Magento 中创建货件时,有一个复选框可让您“通过电子邮件发送货件副本”。 默认情况下未选中。有谁知道我需要编辑哪个文件才能默认设置为“选中”? 最佳答案 这是一个app/design/adm
我有一个简单的 IValueConverter,它只使用 TypeConverter 进行转换。但是,在某些情况下,提供的 TypeConverter 会失败。 如果转换器未提供 Binding,我想
我正在阅读教程,默认 Activity 是一个扩展另一个类的类,它所拥有的只是一个覆盖方法。应用程序如何工作,因为它不做任何其他事情?我很困惑! 最佳答案 父类 Activity 为您处理一切。 关于
我刚刚开始研究游戏框架。我正在尝试构建 rest api,并将 postgresql 用于我的数据库连接。这是我第一次同时使用 play 和 postgre。我在 build.sbt 中建立了一个数据
是否可以创建具有以下属性的 python 对象: class Foo: def __default_method__(x): return x f = Foo() f(10) > 10
我是 jQuery 的新手,遇到了一个烦人的问题。我有一些登录字段,当该字段为空时会填充默认文本,然后在单击时删除。 我的问题是,当用户保存了他们的用户名/密码(使用浏览器)时,如果他们返回页面,登录
考虑这个代码片段: void Foo(std::string str1, std::string str2) {} template void Bar() { Foo(Types{}...);
我正在编写一个简单的 C 程序,我应该用缓冲区溢出来攻击它。所以,我不想在编译时使用任何标志。如何消除使用的默认标志? # readelf -p .GCC.command.line stack Str
考虑这个代码片段: void Foo(std::string str1, std::string str2) {} template void Bar() { Foo(Types{}...);
我有以下代码[这是一道面试题]: #include #include using namespace std; class A{ public: A(){ cout co
我想在 Autofac 中为每个匹配的生命周期范围注册创建一个实例,但偶尔需要从全局容器(没有匹配的生命周期范围)请求一个实例。在不存在匹配生命周期范围的情况下,我想给出一个顶级实例而不是抛出异常。
我正在做一个收集单词共现的修改版本,所以我编写了自己的 javascript,我正在跟踪三个对象中的出现。但是,一旦对象变大(约 800 万、300 万和 172000),每 100000 个句子需要
我正在使用 pykalman 模块中的 KalmanFilter,我想知道它如何处理缺失的观察结果。根据文档: In real world systems, it is common to have
我有一个应用了 RenderTransform 的 Canvas ,如下所示: 谁能告诉我这些值是什么意思?我似乎无法找到用于解析这些值的转换器。 最佳答案 如 RenderTransform是 T
我是 Linux 的新手,现在使用 CentOS 6。我在这里使用 MySQL 工作台,每当我尝试添加新连接时,它都会询问我默认的 key 环密码。我真的不知道,这个密码是从哪里设置的,我之前没有设置
我在 Ubuntu 18.04 上工作。我没有定义 GL_GLEXT_PROTOTYPES .我使用 glXGetProcAddress 加载“核心”OpenGL 函数.我的申请链接到 /usr/li
我按照文档中的示例添加了对使用 asio 加载 HTTPS 站点的支持,这意味着我调用 ctx.set_default_verify_paths();使用系统默认路径来查找证书。 然而,我得到:una
我是一名优秀的程序员,十分优秀!