如何使用类型类来实现持久化、内省、身份、打印、
在关于面向对象的神话的讨论中,Tim Sweeney 描述了他认为是我们大家所使用的包罗万象的框架的一个很好的替代方案今天使用。
他似乎对类型类最感兴趣:
我们可以使用类型类之类的结构来定义功能(例如持久性、内省、 身份、打印)正交于类型构造,如类和 接口
我对类型类作为“类型的类型”非常熟悉,但我不确定它们将如何应用于上述问题:持久性、打印……
有什么想法吗?
In the discussion on The Myths of Object-Orientation, Tim Sweeney describes what he thinks is a good alternative to the all-encompassing frameworks that we all use today.
He seems most interested in typeclasses:
we can use constructs like typeclasses to define features (like persistence, introspection,
identity, printing) orthogonally to type constructs like classes and
interfaces
I am passingly familiar with type classes as "types of types" but I am not sure exactly how they would be applied to the fore-mentioned problems: persistence, printing, ...
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最好的猜测是通过默认方法重用代码,并通过将类型类实现与类型本身分离来进行正交定义。
基本上,当您定义类型类时,您可以定义方法的默认实现。例如 Haskell 中的
Eq
(相等)类将/=
(不等于)定义为not (x == y)
并且此方法将起作用默认情况下适用于该类型类的所有实现。在其他语言中以类似的方式,您可以定义一个类型类,其中编写了除一两个方法之外的所有持久性代码(Save
、Load
)。或者,在具有良好反射能力的语言中,您可以提前定义所有持久性方法。实际上,它有点类似于多重继承。现在,另一件事是,您不必在定义类型的同一位置将类型类附加到您的类型,您实际上可以稍后在不同的位置执行此操作。这使得持久性逻辑可以很好地与原始类型分开。
在我最喜欢的论文中,有一些关于 OOP 语言的很好的例子: http:// www.stefanwehr.de/publications/Wehr_JavaGI_generalized_interfaces_for_Java.pdf。他们对默认实现和追溯接口实现的描述本质上与我刚才描述的语言特性相同。
免责声明:我不太了解 Haskell,所以我可能在某些地方是错误的
My best guess would be code reuse through default methods and orthogonal definition through detachment of type class implementation from type itself.
Basically, when you define type class, you can define default implementations for methods. For example
Eq
(equality) class in Haskell defines/=
(not equal) asnot (x == y)
and this method will work by default for all implementation of the type class. In a similar way in other language you could define a type class with all persistence code written (Save
,Load
) except for one or two methods. Or, in a language with good reflection capabilities you could define all persistence methods in advance. In practice, it is kind of similar to multiple inheritance.Now, the other thing is that you do not have to attach the type class to your type in the same place where you define your type, you can actually do it later and in a different place. This allows persistence logic to be nicely separated from the original type.
Some good examples in how that looks like in an OOP language are in my favorite paper ever: http://www.stefanwehr.de/publications/Wehr_JavaGI_generalized_interfaces_for_Java.pdf. Their description of default implementations and retroactive interface implementations are essentially the same language features as I have just described.
Disclaimer: I do not really know Haskell so I might be wrong in places