Add a variable to a class instance at runtime: Difference between revisions
Content added Content deleted
Line 452: | Line 452: | ||
newvar (some value) |
newvar (some value) |
||
-> $385605941</lang> |
-> $385605941</lang> |
||
=={{header|Pike}}== |
|||
Pike does not allow adding variables to existing classes, but we can design a class that allows us to add variables. |
|||
<lang Pike>class CSV |
|||
{ |
|||
mapping variables = ([]); |
|||
mixed `->(string name) |
|||
{ |
|||
return variables[name]; |
|||
} |
|||
void `->=(string name, mixed value) |
|||
{ |
|||
variables[name] = value; |
|||
} |
|||
array _indices() |
|||
{ |
|||
return indices(variables); |
|||
} |
|||
} |
|||
object csv = CSV(); |
|||
csv->greeting = "hello world"; |
|||
csv->count = 1; |
|||
csv->lang = "Pike"; |
|||
indices(csv); |
|||
Result: ({ /* 3 elements */ |
|||
"lang", |
|||
"count", |
|||
"greeting" |
|||
}) |
|||
</lang> |
|||
=={{header|Pop11}}== |
=={{header|Pop11}}== |