使用“apply()” 在specman中返回void的方法
Specman 具有 apply()
方法来对列表的所有元素执行相同的操作:
var a: list of int;
a = somefunction.that.returns.list.of.int();
var b:= a.apply(it * 2);
其中 apply()
的作用与:
for each in a {
b.add(it.*2);
};
现在,如果我想调用方法对 a
的元素进行操作,只要该方法返回一个值,我就可以使用 apply()
。 但是,如果我有:
struct bar {
x: int;
foo() is {
message(LOW, "x is ", x);
};
};
并且我尝试这样做:
var a: list of bar;
a = somefunction.that.returns.list.of.bar();
a.apply(it.foo());
它不会编译,因为 foo()
返回 void
。 相反,我必须使用显式循环:
for each in a {
it.foo();
};
specman 中是否有类似于 apply()
不需要返回值的方法?
Specman has the apply()
method to perform the same action on all elements of a list:
var a: list of int;
a = somefunction.that.returns.list.of.int();
var b:= a.apply(it * 2);
Where apply()
does the same as:
for each in a {
b.add(it.*2);
};
Now, if I want to call a method on the elements of a
, I can use apply()
so long as the method returns a value. However, if I have:
struct bar {
x: int;
foo() is {
message(LOW, "x is ", x);
};
};
And I try to do:
var a: list of bar;
a = somefunction.that.returns.list.of.bar();
a.apply(it.foo());
It doesn't compile, because foo()
returns void
. Instead, I have to use an explicit loop:
for each in a {
it.foo();
};
Is there a method in specman similar to apply()
that doesn't require a return value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这里的基本问题是你想要误用
apply()
。 我想说这个函数有一些函数式编程背景,其目的是对列表中的每个项目执行一些操作并返回一个新列表(例如map Python 或 Perl)。
如果您对函数调用的副作用(如果函数不返回值)感兴趣,那么使用显式循环更为正确。 另请参阅 使用 map() 与 for 是否有价值?
这表示我目前想不出不同的解决方案。 也许将 foo() 包装在返回值的函数中,但这绝对看起来超载。
I think the basic problem here is that you want to mis-use
apply()
. I would say this function has some functional programming background and its purpose is to do something which each item of a list and return a new list (likemap
in Python or Perl).If you are interested in the side-effects of a function call, which you are if the function doesn't return a value, its more correct to use the explicit loop. See also Is there a value in using map() vs for?
This said I can't think of a different solution at the moment. Maybe wrapping
foo()
in a value-returning function but this definitely seems overload.