¿Es posible pasar un mensaje a la instancia de una clase desde dentro de un método?
Aquí hay un ejemplo de lo que estoy tratando de hacer:
Define Class(
"example",
x=1;
test = Method( {name, method},
<< Insert(name, method);
);
);
t = New Object( example());
// This fails to insert the blah method
t:test("blah", Method({y}, show("Blah " || char(x) || char(y))));
t:blah(6);
//This succeeds in inserting and calling the blah method
t << Insert("blah", Method({y}, show("Blah " || char(x) || char(y))));
t:blah(6);Sospeché que podría necesitar escribir explícitamente "este << Insertar", pero eso produce un error diferente:
Define Class(
"example",
x=1;
test = Method( {name, method},
<STRONG>this << Insert(name, method);</STRONG>
);
);
t = New Object( example());
// This fails because "send expects scriptable object"
t:test("blah", Method({y}, show("Blah " || char(x) || char(y));));
t:blah(6);También consideré llamar explícitamente a Enviar (no se puede agregar el método) y esto: Enviar (no se puede resolver el nombre Enviar).
También consideré llamar directamente a esto: Insertar (no puede resolver el nombre Insertar) e Insertar (agrega con éxito el método blah, pero no logra sondear correctamente el mecanismo de llamada, por lo que no se pasan argumentos al método).
Motivación:
Estoy haciendo esto porque estoy tratando de crear automáticamente métodos getter y setter para una lista de propiedades de clase.Idealmente, podré hacer lo siguiente:
Define Class(
"example",
_init_ = Method( {assocArray},
For Each({property, updateMethod}, assocArray,
Insert(key, Eval Expr(Method({value}, TrySetProperty(Expr(property), value, Expr(updateMethod)))));
);
);
TrySetProperty=Method({parameter, value, expressionIfChanged},
match(value,
., ., //Do nothing if missing value
this << Get Value(parameter), ., //Do nothing if unchanged value
this << Insert(parameter, value); //Otherwise update the value
Eval(expressionIfChanged); //and eval the expression
);
);
);
properties = Associative Array({{"prop1", Expr(ThingToDoOnUpdate1)},{"prop2", Expr(ThingToDoOnUpdate2)}});
t = New Object( example(properties));