Anonymous user
Multiple distinct objects: Difference between revisions
Nimrod -> Nim
m (→{{header|J}}: fix lang tag) |
(Nimrod -> Nim) |
||
Line 574:
Modula-3 does not define what values the elements of A have, but it does guarantee that they will be of type T.
=={{header|
The simplest form of initialization works, but is a bit cumbersome to write:
<lang
echo "Foo()"
"mystring"
Line 584:
for i in 0 .. <n: ws[i] = foo()</lang>
If actual values instead of references are stored in the sequence, then objects can be initialized like this. Objects are distinct, but the initializer <code>foo()</code> is called only once, then copies of the resulting object are made:
<lang
result = newSeq[T] len
for i in 0 .. <len:
Line 591:
var xs = newSeqWith(n, foo())</lang>
To get the initial behaviour, where <code>foo()</code> is called to create each object, a template can be used:
<lang
var result {.gensym.} = newSeq[type(init)](len)
for i in 0 .. <len:
|