专注于关键词的通用函数(Generic Function Specializing on Keywords)

编程入门 行业动态 更新时间:2024-10-24 18:24:36
专注于关键词的通用函数(Generic Function Specializing on Keywords)

在Common Lisp中有没有一种方法可以创建一个泛型函数,其中专用于参数的参数是关键字参数?

例如,而不是这个:

CL-USER> (defgeneric tst (a)) #<STANDARD-GENERIC-FUNCTION TST (0)> CL-USER> (defmethod tst ((a list)) (print a)) #<STANDARD-METHOD TST (LIST) {1004ECC903}>

..在参数'a'上指定。 我想要有以下几点:

CL-USER> (defgeneric tst (&key a)) #<STANDARD-GENERIC-FUNCTION TST (0)> CL-USER> (defmethod tst (&key (a list)) (print a)) #<STANDARD-METHOD TST (LIST) {1004ECC903}>

显然,这与设置关键字参数的默认值的语法冲突,所以我有点卡住了。 在实际的代码中,这个问题与我选择了不同的参数布局有关,所以这不是问题(并且引导起来有点整洁!)但是如果我再遇到这个问题,我想我最好问专家!

欢呼一切

Is there a way in Common Lisp to create a generic function where the argument to specialize on is a keyword argument?

For example rather than this:

CL-USER> (defgeneric tst (a)) #<STANDARD-GENERIC-FUNCTION TST (0)> CL-USER> (defmethod tst ((a list)) (print a)) #<STANDARD-METHOD TST (LIST) {1004ECC903}>

..which specifies on the argument 'a'. I'd like to have the following:

CL-USER> (defgeneric tst (&key a)) #<STANDARD-GENERIC-FUNCTION TST (0)> CL-USER> (defmethod tst (&key (a list)) (print a)) #<STANDARD-METHOD TST (LIST) {1004ECC903}>

Obviously this clashes with the syntax for setting the default values of keyword arguments so I'm a tad stuck. In the actual code this problem relates to I chose a different argument layout so this wasn't an issue (and was a bit tidier to boot!) but in case I run into this again I thought I best ask the experts!

Cheers all

最满意答案

不,这不是Common Lisp提供的。

No, that's not provided by Common Lisp.

更多推荐

本文发布于:2023-08-04 09:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415325.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   关键词   专注于   Generic   Specializing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!