Anonymous user
Multiple distinct objects: Difference between revisions
added perl
(added perl) |
|||
Line 178:
Array.init n (fun _ -> new foo);;
</code>
=={{header|Perl}}==
incorrect:
<code perl>
(Foo->new) x $n # here Foo->new can be any expression that returns a reference representing a new object
</code>
which is incorrect since <tt>Foo->new</tt> is only evaluated once.
A correct version is:
<code perl>
map { Foo->new } 1 .. $n;
</code>
which evaluates <tt>Foo->new</tt> <var>$n</var> times and collects each result in a list.
=={{header|Python}}==
|