什么是谓词调度 [英] What is Predicate Dispatch

查看:55
本文介绍了什么是谓词调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我在Clojure中看到了很多有关谓词分发的讨论,并且想知道这件事是否有事.换句话说,谓词分派是什么,它与泛型函数,OOP多态性和模式有何不同?谢谢

I have seen much talk about predicate dispatch in Clojure lately and wonder if there is something to this thing. In other words, what is predicate dispatch and how does it differ from generic functions, OOP polymorphism, and patterns? Thank you

推荐答案

编辑:Clojure多方法不是谓词调度.

Edited: Clojure multimethods are not predicate dispatch.

在传统的面向对象程序设计中,多态意味着您可以具有一个方法的多个实现,并且所调用的确切实现由调用该方法的对象的 type 决定..这是类型调度.

In traditional object-oriented programming, polymorphism means that you can have multiple implementations of a method, and the exact implementation that gets called is determined by the type of the object on which you called the method. This is type dispatch.

Clojure 多种方法对此进行了扩展,以便任意函数可以确定调用哪个实现.在Clojure形式(defmulti name f)中,函数 f dispatch函数.

Clojure multimethods extend this so that an arbitrary function can decide which implementation gets called. In the Clojure form (defmulti name f), the function f is the dispatch function.

调度功能可以是 class ,在这种情况下,您需要返回类型dispatch.但是该功能可能还有其他用途:计算调度值,在数据库中查找内容,甚至调出Web服务.

The dispatch function could be class, in which case you're back to type dispatch. But that function could be anything else: computing a dispatch value, looking up stuff in a database, even calling out to a web service.

真正的谓词分派可能会允许每个方法实现指定一个或多个分派函数(谓词)来决定何时应用该方法.这比多方法更通用,但实现起来更复杂.Clojure不支持它.

True predicate dispatch would potentially allow each method implementation to specify one or more dispatch functions (predicates) to decide when that method applies. This is more general than multimethods but more complicated to implement. Clojure does not support it.

泛型函数是来自其他Lisps的术语.例如,Common Lisp提供了通用函数,该函数可以按类型加受限制的集合进行调度其他功能.

Generic function is a term from other Lisps. Common Lisp, for example, provides generic functions which can dispatch on type plus a restricted set of other functions.

这篇关于什么是谓词调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆