问题标签 [mop]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
grails - 将 Groovy MOP 委托连接到视图 - StackOverflowError
我是 Groovy 和 Grails 的新手,并试图通过查看这篇文章来实现委托:http: //groovy.codehaus.org/Replace+Inheritance+with+Delegation。
使用 Netbeans 中默认生成的视图,当我遵循继承和委托示例时,它创建了适当的表单字段,但在 MOP 示例中,它只创建了局部变量字段。如果我使用前面示例生成的表单,我会收到 StackOverflowError。
如何将视图连接到 MOP 示例?
更新:添加视图、域类和控制器类
领域:
人:
视图(在继承时生成):
使用 MOP 生成的视图:
控制器类:
groovy - 在 groovy @Canonical bean 构造函数调用中添加缺少的属性?
我是 groovy 的新手,刚刚开始探索它的元编程功能。我被困在 bean 构造函数调用中添加缺少的属性。
在要与 FactoryBuilderSupport 一起使用的类中,我想动态添加那些在构造函数调用期间尚未定义和提供的属性。这是精简版:
但是,如果我构造具有未知属性的类,则不会添加该属性,而是得到一个MissingPropertyException:
属性持续时间不存在,我希望它可以通过propertyMissing. 据我了解 groovy,调用元组构造函数会导致无参数构造函数调用,然后调用 groovy 生成的设置器。那为什么我会得到一个MissingPropertyException?
由于我是 groovy 的新手,我可能缺少一些基本的 AST 或 MOP 规则。我非常感谢您的帮助。
groovy - 在 Groovy 中拥有可重用元类代码的优雅方式是什么?
我想对我的一些类应用元编程转换,比如说通过添加 printXxx 方法,如下所示:
我有一个粗略的想法,一旦我有了一个元类,如何做到这一点:
现在我如何把这段代码变成一个可重用的“库”?我想做类似的事情:
或者
perl - 为什么 Moose Role 不排除特定的角色属性?
我有一个 Moose::Role (除其他外):
然后,在使用该角色的模块中,
然后,在一个脚本中,我实例化了 Some::Module 的一个实例:
我可以打电话
但是,我无法打电话
正如我所期望的那样,调用 $some_module->table 会导致脚本停止。调用 $some_module->id 不会。
当我使用 Data::Dumper 转储 $some_module 元类的属性列表时,它显示 id 属性已定义但 table 属性未定义。
有谁知道为什么 Role 中定义的 'id' 属性不会从元类中排除,但 'table' class_attribute 会?如上所述,问题在于 Some::Module 的用户在需要调用 module_id() 时可以调用 id()。
此外,当转储 $some_module 对象时,“id”不会出现在转储中。
编辑:
这是一个说明问题的示例。我已经定义了一个实现 id 的角色,然后我正在使用包 My::Product 中的角色。但是,我在使用 id 时将其排除在外。当我从元对象打印属性时,它表明它实际上存在。我的印象是,在使用角色时从角色中排除 id 将不允许调用它。我希望它不仅不在元对象中,而且在尝试调用它时也会死掉。
编辑 2:在转储 $attr 后,我看到 first_name 和 id 在 method_exclusions 中。
common-lisp - 自定义插槽选项不会对其参数应用任何缩减
假设我定义了一个使用验证器插槽增强标准插槽的元类,当我:validator (clavier:valid-email "The email is invalid")作为选项传递时,它不是存储表达式的结果(这是一个可调用函数),而是存储表达式本身。扩展标准插槽时我错过了一步吗?如何确保在存储之前评估表达式?我正在使用 SBCL 1.2.11 顺便说一句。这是有问题的代码
当创建实例时代码失败,因为 (CLAVIER:VALID-EMAIL "The email is invalid") 不是可调用的。
methods - common lisp 中的匿名方法
我想将通用函数保存为变量:
call-next-method但是当添加一个方法时,我必须next-method-p自己定义:
如何调用定义方法call-next-method?有没有更简单的方法来做到这一点?
groovy - 对 Groovy MOP 中的 invokeMethod 方法感到困惑
首先看下面的 Groovy 代码:
输出是:
根据Groovy的方法调度机制,我认为应该直接调用Car中的start方法,而不是被Car的metaClass中的invokeMethod拦截。为什么start方法被invokeMethod拦截了?在对象上调用方法时,invokeMethod 是如何调用的?
如果你能给我一些关于 Groovy 方法调度机制(MOP)的详细解释,我将不胜感激。
common-lisp - 获取类优先级列表时未绑定插槽?
为什么我不能在 sbcl 中获得一个简单的类优先级列表?
common-lisp - Changing method dispatch in Common Lisp
I'm trying to simulate something akin to Haskell's typeclasses with Common Lisp's CLOS. That is, I'd like to be able to dispatch a method on an object's "typeclasses" instead of its superclasses.
I have a metaclass defined for classes which have and implement typeclasses(which are just other classes). Those classes(those that implement typeclasses) have a slot containing the list of the typeclasses they implement.
I'd like to be able to define methods for a typeclass, and then be able to dispatch that method on objects whose class implement that typeclass. And I'd like to be able to add and remove typeclasses dynamically.
I figure I could probably do this by changing the method dispatch algorithm, though that doesn't seem too simple.
Anybody is comfortable enough with CLOS and the MOP to give me some suggestions?
Thanks.
Edit: My question might be specified as, how do I implement compute-applicable-methods-using-classes and compute-applicable-methods for a "custom" generic-function class such that if some of the specializers of a generic function method are typeclasses(classes whose metaclass is the 'typeclass' class), then the corresponding argument's class must implement the typeclass(which simply means having the typeclass stored in a slot of the argument's class) for the method to be applicable?
From what I understand from documentation, when a generic function is called, compute-discriminating-functionis first called, which will first attempt to obtain applicable methods through compute-applicable-methods-using-classes, and if unsuccessful, will try the same with compute-applicable-methods.
While my definition of compute-applicable-methods-using-classes seems to work, the generic function fails to dispatch an applicable function. So the problem must be in compute-discriminating-function or compute-effective-method.
See code.
json - 如何拦截地图getProperty和list getAt?
我正在抓取外部资源,主要是 JSON。我new JsonSlurper().parse(body)用来解析它们,并使用诸如def name = json.user[0].name. 这些是外部的,可以在没有通知的情况下更改,所以我希望能够检测到这一点并以某种方式记录它。
在阅读了一些关于 MOP 的信息后,我认为如果缺少属性,我可以更改地图和列表的适当方法以记录。我只想json递归地执行对象及其属性。问题是,我不知道该怎么做。
或者,有没有更好的方法来完成这一切?
[编辑] 例如,如果我得到这个 JSON:
但是,如果他们将“所有者”更改为“所有者信息”,则上述访问将引发 NPE。我想要的是拦截访问并做一些事情(比如将它记录在一个特殊的日志中,或者其他什么)。我也可以决定抛出一个更专业的异常。
我不想捕获 NullPointerException,因为它可能是由我的代码中的一些错误而不是更改的数据格式引起的。此外,如果他们将“firstName”更改为“givenName”,但保留“owners”名称,我只会得到一个null值,而不是NPE. 理想情况下,我也想检测这种情况。
如果可能的话,我也不想放置很多 if 或 evlis 运算符。
我实际上设法拦截了地图:
我仍然无法找到如何为列表执行此操作: