RosettaCodeData/Task/Multiple-distinct-objects/Pluto/multiple-distinct-objects.pluto

33 lines
858 B
Text
Raw Permalink Normal View History

2026-04-30 12:34:36 -04:00
local foo_count = 0
class foo
private number
function __construct()
foo_count += 1 -- increment object counter
self.number = foo_count -- allocates a unique number to each object created
end
function getNumber() return self.number end
end
local n = 10 -- say
-- Create a List of 'n' distinct foo objects
local foos = table.create(n)
for i = 1, n do foos[i] = new foo() end
-- Show they're distinct by printing out their object numbers
foos:foreach(|f|-> do
io.write($"{f:getNumber()} ")
end)
print("\n")
-- Now create a second List where each of the 'n' elements is the same foo object
local foos2 = table.create(n)
local foo = new foo()
for i = 1, n do foos2[i] = foo end
-- Show they're the same by printing out their object numbers.
foos2:foreach(|f| -> do
io.write($"{f:getNumber()} ")
end)
print()