Anonymous user
Multiple distinct objects: Difference between revisions
Add Logtalk implementation
(Added Erlang) |
(Add Logtalk implementation) |
||
Line 451:
for (var i = 0; i < n; i++)
a[i] = new Foo();</lang>
=={{header|Logtalk}}==
Using prototypes, we first dynamically create a protocol to declare a predicate and then create ten prototypes implementing that protocol, which one with a different definition for the predicate:
<lang logtalk>
| ?- create_protocol(statep, [], [public(state/1)]),
findall(
Id,
(integer::between(1, 10, N),
create_object(Id, [implements(statep)], [], [state(N)])),
Ids
).
Ids = [o1, o2, o3, o4, o5, o6, o7, o8, o9, o10].
</lang>
Using classes, we first dynamically create a class (that is its own metaclass) to declare a predicate (and define a default value for it) and then create ten instances of the class, which one with a different definition for the predicate:
<lang logtalk>
| ?- create_object(state, [instantiates(state)], [public(state/1)], [state(0)]),
findall(
Id,
(integer::between(1, 10, N),
create_object(Id, [instantiates(state)], [], [state(N)])),
Ids
).
Ids = [o1, o2, o3, o4, o5, o6, o7, o8, o9, o10].
</lang>
=={{header|Mathematica}}==
|