RosettaCodeData/Task/Dynamic-variable-names/Sidef/dynamic-variable-names.sidef

11 lines
308 B
Plaintext

var name = read("Enter a variable name: ", String); # type in 'foo'
class DynamicVar(name, value) {
method init {
DynamicVar.def_method(name, ->(_) { value })
}
}
var v = DynamicVar(name, 42); # creates a dynamic variable
say v.foo; # retrieves the value