Common Lisp 对象系统 (CLOS) 是否支持鸭子类型?
我正在阅读“Practical Common Lisp”,我想知道 Common Lisp 是否像 Ruby 一样支持 Duck-Typing?
在 Ruby 中,无论类如何,都可以调用对象上的方法,只要该对象实现具有调用者假定的名称和参数列表的方法即可。
那么克洛斯呢?是否可以简单地假设通用函数可以处理它,从而在不考虑其类的情况下调用对象上的方法。也许不需要鸭子类型,因为 CLOS 不遵循消息传递哲学,并且方法不绑定到类。
I'm reading "Practical Common Lisp" and I wonder if Common Lisp supports Duck-Typing like e.g. Ruby?
In Ruby it's possible to call a method on an object regardless of the class as long as it implements a method with the name and argument list that the caller assumes.
What about CLOS? Is it possible to call methods on objects without considering their class simply by assuming that a generic function will cope with it. Perhaps duck-typing is not needed because CLOS doesn't follow a message passing philosophy and methods are not bound to classes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实确实如此。每个通用函数都可以动态地专门用于某个类。还可以有一个默认实现。由于 Lisp 使用动态类型,因此每个函数都可以使用任何类型的参数来调用,并且对于泛型函数,基于参数类型的调度决策是在运行时做出的。
That is exactly the case. Every generic function can be dynamically specialized for a certain class. There can also be a default implementation. And since Lisp uses dynamic typing, every function can be called with arguments of any type, and for generic functions the dispatch decision, based on the type of argument, is taken at runtime.