- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在学习 CLOS 中的通用函数。
由于我在教科书和网上找到的示例类型,我感到非常困惑。这些示例始终使用存在多个分派(dispatch)的事实。根据参数类型,执行不同的计算。但是,为什么示例中从未使用过参数本身?
Example code from Wikipedia
; declare the common argument structure prototype
(defgeneric f (x y))
; define an implementation for (f integer t), where t matches all types
(defmethod f ((x integer) y) 1)
(f 1 2.0) => 1
; define an implementation for (f integer real)
(defmethod f ((x integer) (y real)) 2)
(f 1 2.0) => 2 ; dispatch changed at runtime
在上面的示例中,您可以看到方法本身实际上从未使用过 x
或 y
变量。所有这些示例从不使用变量是巧合吗?可以用吗?
此外,它写在 Wikipedia 上:
Methods are defined separately from classes, and they have no special access (e.g. "this", "self", or "protected") to class slots.
好的,所以方法没有“this”,因为它们不属于某个类。但是为什么泛型函数方法可以有一个接收者呢?接收者不是类似于类中的'this'吗?
最佳答案
当然,您可以从参数列表中访问变量。维基百科示例仅用于说明哪个方法返回值。
But why can generic-function methods have a receiver then? Isn't the receiver similar to the 'this' in a class?
CLOS 通用函数没有单个接收器,因此使用接收器 这个词没有意义。您提到的实现可能没有实现完整的 CLOS,而是没有多重分派(dispatch)的变体。
CLOS 示例:
CL-USER 8 > (defmethod plus ((s1 string) (s2 string))
(concatenate 'string s1 s2))
#<STANDARD-METHOD PLUS NIL (STRING STRING) 4020001E6B>
CL-USER 9 > (plus "foo" "bar")
"foobar"
您会看到使用了变量 s1
和 s2
。将其中之一命名为 receiver
是没有意义的。
但您可以随意命名变量,当您的应用程序仅对第一个参数使用分派(dispatch)时,您可能希望调用该变量 receiver
,但该名称对于 CLOS 没有语义。这只是另一个名字。
通常对于 CLOS 代码,最好为参数提供有用的名称。
这是误导,因为我们没有在 CLOS 中进行消息传递:
(defmethod plus ((receiver string) (argument string))
(concatenate 'string receiver argument))
这个更有用:
(defmethod plus ((string-1 string) (string-2 string))
(concatenate 'string string-1 string-2))
关于lisp - 不能在泛型函数方法中使用变量吗? (CLOS/LISP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41474877/
任何 CL'er 都可以解释一下 CLOS 中的“插槽”吗?我发现很难理解插槽名称后面的部分。那是在: (defclass foo() (data1 :initarg foo)) “initarg”和
有一个通用方法,比如 incx . incx有两个版本.一种专攻类型 a ,还有一个专门针对类型 b .类型 b是 a 的子类.你得到一个类型为 b 的对象,派生类型 - 但您想调用专门用于类型 a
在阅读 CLOS(Paul Graham 在 ANSI Common Lisp 中)时,我注意到有九个函数可以作为第二个参数提供给 defmethod:+, and, append, list, ma
当类在列表中时,我无法使用 clos 访问器函数。 假设我有 a 类: (defclass a () ((a :accessor a :initarg :a))) 我制作了 2 个实例
我对使用 CLOS 还很陌生。在这里,我写了一种使用 CLOS 定义队列的可能方法: (defclass Queue () ((queue-size :reader queue
你会如何用 Lisp 表达下面的 Java 代码? class Foo { private String s; public Foo(String s) { this.
我正在尝试创建一个类,该类可以将符号向量存储在 SBCL 的槽中。我不知道如何设置它。 到目前为止我最好的猜测是 (defclass Individual () ((discrete-decisi
我正在寻找一种以浅层方式克隆 CLOS 对象的方法,因此创建的对象将具有相同的类型,在每个插槽中具有相同的值,但是是一个新实例。我发现的最接近的是一个标准函数复制结构,它对结构执行此操作。 最佳答案
我正在学习 CLOS 中的通用函数。 由于我在教科书和网上找到的示例类型,我感到非常困惑。这些示例始终使用存在多个分派(dispatch)的事实。根据参数类型,执行不同的计算。但是,为什么示例中从未使
有没有类似CLOS的东西(Common Lisp 对象系统)用于 Clojure? 最佳答案 您是否考虑过 Clojure 的 data types (尤其是 defrecord),protocols
我正在学习构建我的 CL 程序,现在在使用大型包进行编程时无法使用 CLOS。 包.lisp (defpackage :my-project.a (:use :cl) (:export
我有以下类(class): (defclass category () ((cat-channel-name :accessor cat-channel-name :initarg :
我遇到过这样的说法,即 Common Lisp 对象系统 (CLOS) 优于传统的(基于类的)面向对象系统。 Wikipedia entry for CLOS提到了这两种方法之间的差异——主要是多重分
访问类槽时,而不是写入 (defmethod get-name ((somebody person) (slot-value somebody 'name)) 是否可以使用点符号(又名 C++),即
在 Common Lisp 中,是否有一种白话,可以像在 Java 中那样为一次性的“小型接口(interface)实现者”定义匿名类? 例如, this.addListener(new Listen
我想知道为什么 Common Lisp 中没有内置的相等运算符来比较 CLOS 对象(标准类)。例如,“equalp”可以应用于数组、结构、哈希表,但不能应用于对象。 我假设一个新的测试下降一个对象并
我有一个 CLOS 对象的插槽名称列表: (DEFCLASS TRIAL-DATA (STANDARD-OBJECT) ((A-DATUM :ACCESSOR A-DATUM :INITARG :A
我有一个 Node 类,它有一个“元素”插槽,其中包含一个包含数字和一个字母的列表,例如: '(1 2 3 b 4 5 6) (defclass node () ((element :reader
我正在尝试在 Common Lisp 中执行一个多重方法的“重载调用”。这是案例的简化摘要: (defclass foo () ((slotty :accessor slotty :initarg
我需要一些帮助来理解以下代码的执行顺序。 我创建了一个 pie 的实例,使用以下内容: (cook (make-instance 'pie)) 我知道 lisp 从最具体到最不具体的顺序执行函数。但是
我是一名优秀的程序员,十分优秀!