- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我怎么知道在使用 FALLBACK
时我是否真的需要返回一个 l 值? ?
我正在使用 return-rw
但我只想使用 return
在可能的情况。我想跟踪我是否真的修改过 %!attrs
或仅在 FALLBACK
时才读取值被称为。
或者(替代计划 B)我可以附加一个回调或类似于我的 %!attrs
的东西吗?监控变化?
class Foo {
has %.attrs;
submethod BUILD { %!attrs{'bar'} = 'bar' }
# multi method FALLBACK(Str:D $name, *@rest) {
# say 'read-only';
# return %!attrs{$name} if %!attrs«$name»:exists;
# }
multi method FALLBACK(Str:D $name, *@rest) {
say 'read-write';
return-rw %!attrs{$name} if %!attrs«$name»:exists;
}
}
my $foo = Foo.new;
say $foo.bar;
$foo.bar = 'baz';
say $foo.bar;
最佳答案
这感觉有点像一个 X-Y 问题,所以让我们简化这个例子,看看这个答案是否有助于你的决定。
首先:如果您返回散列中不存在的键的“值”,实际上您正在返回一个容器,当分配给时,该容器将自动激活散列中的键:
my %hash;
sub get($key) { return-rw %hash{$key} }
get("foo") = 42;
dd %hash; # Hash %hash = {:foo(42)}
return-rw
在这里确保返回实际的容器,而不仅仅是容器中的值。或者,您可以使用
is raw
trait,它允许你只设置最后一个值:
my %hash;
sub get($key) is raw { %hash{$key} }
get("foo") = 42;
dd %hash; # Hash %hash = {:foo(42)}
return
在那种情况下,因为它仍然会再次去容器化。
I want to track if I've actually modified
%!attrs
or have only just read the value whenFALLBACK
was called.
class Foo {
has %!attrs;
has %!unexpected;
method TWEAK() { %!attrs<bar> = 'bar' }
method FALLBACK(Str:D $name, *@rest) is raw {
if %!attrs{$name}:exists {
%!attrs{$name}
}
else {
%!unexpected{$name}++;
Any
}
}
}
Any
.
关于metaprogramming - 使用 `FALLBACK` 时如何知道是否返回左值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58071806/
我想在宏内部构建一个带有关键字参数的构造函数,并且第一个关键字参数需要用于表达式。我无法将该表达式放入表达式中。这就是我的意思。说我有一个类型 type Test ex end 其中包含一个表达式
在最长的时间里,我想设计一种将可扩展性与效率(以及安全性,易用性等)结合在一起的编程语言,最近我重新发现了D,我想知道D 2.0是否真的是我想使自己成为自己的语言? 。我最喜欢的是元编程的潜力。从理论
我正在尝试在coffee脚本中动态创建方法,但是如我的代码所示,用于创建方法的迭代器不会在两次迭代之间重置其变量,因此我遇到了冲突的共享变量: class MyClass constructor:
我正在尝试使用元编程创建一个函数来计算方程组的残差。 这是我到目前为止尝试过的(玩具示例): function syst!(x::Vector, ou::Vector) for i in 1:
我对 Julia 很陌生,所以对于我可能对语言的任何误解,我深表歉意。我最近主要使用 Python,并大量使用 SymPy 及其代码生成功能,看起来 Julia 及其元编程功能是专为以我喜欢的风格编写
有人可以清楚地说明在Julia中可用于元编程的各种报价机制,并用一个最小的例子来说明每个报价机制吗? 这样就很清楚在哪种情况下使用哪个... 据我所知: :(someExpr; maybeMore)
如何动态地向类添加属性?我尝试了此方法,但是它提示缺少方法,因此我不确定为什么,因为我没有尝试添加方法。 use v6.d; class Foo does Metamodel::AttributeCo
我最近遇到了Polymorphic Code一词,想知道是否有人建议使用合法的(即在法律和商业适用软件中)在计算机程序中使用它的理由?链接到现实世界的例子,将不胜感激! 在有人回答之前,请告诉我们有关
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
我已经按照 SO 问题的答案中的说明进行操作 How can classes be made parametric in Perl 6? .但是,我遇到了一些软障碍。我正在尝试使用类型捕获键入内部类的
我正在阅读 TheServerSide 上的一篇文章 ployglot programming on the Java platform 。文章中的一些评论将元编程称为生成代码的能力(可能是动态生成的
我们都知道MetaProgramming是代码==数据(或编写程序的程序)的概念。 但是,有没有使用它的应用程序?使用它的好处是什么? 该问题可以关闭,但我没有看到任何相关问题。 最佳答案 IDE充满
假设我有一个映射(具有已知类型),例如 1:错误,4:假的,8:是的,16:真 我想生成一个接受输入并给出正确输出的函数。我不关心不在上述映射中的任何输入会发生什么,例如 3 永远不会被期望。 例如,
我正在寻找 Julia 中与 Matlab 相媲美的一些功能 save('myfile.mat', 'myvar1', 'myvar2') 比如使用HDF5.jl,很容易做到 @write filen
我只是出于好奇问这个: 是否有任何工具可以自动将具有合理复杂性的源代码从一种语言转换为另一种语言? 有没有可以编译成其他几种语言的“元语言”?例如 CoffeeScript 编译成 Javascrip
所以元编程——你可以在运行时修改类/对象,注入(inject)新的方法和属性的想法。我知道它对框架开发有好处;一直在使用 Grails,并且该框架在运行时为您的类添加了一堆方法。您在 User 对象上
如果有人能解释为什么在 Smalltalk 中,表达式会很好 class := Class new name: 'OurClass'; superclass: Object 不适合创建新类。更
我的模块中有三个相同的方法,它们(几乎)做同样的事情。我没有重复函数定义,而是尝试定义它们一次,以使所有代码都保持最小和相同。 到目前为止,我已经尝试使用 Code.eval_string : def
假设我想用一个宏覆盖每个函数调用,该宏计算我调用每个特定函数的次数。 在 Elixir 中可能吗? 我知道有可能覆盖内置宏和函数 import Kernel, except: [name: arity
假设我制作了这个简单的字符串宏 macro e_str(s) return string("I touched this: ",s) end 如果我将它应用于带有插值的字符串,我 获得: ju
我是一名优秀的程序员,十分优秀!