Common Lisp 对象系统 (CLOS) 是否支持鸭子类型?

发布于 2025-01-06 07:27:29 字数 237 浏览 1 评论 0原文

我正在阅读“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半夏半凉 2025-01-13 07:27:29

也许不需要鸭子类型,因为 CLOS 不遵循消息传递原理,并且方法不绑定到类。

事实确实如此。每个通用函数都可以动态地专门用于某个类。还可以有一个默认实现。由于 Lisp 使用动态类型,因此每个函数都可以使用任何类型的参数来调用,并且对于泛型函数,基于参数类型的调度决策是在运行时做出的。

Perhaps duck-typing is not needed because CLOS doesn't follow a message passing philosophy and methods are not bound to classes.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文