using “apply()” with methods that return void in specman
Question
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?
Solution
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 (like map
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.