È possibile passare un messaggio all'istanza di una classe dall'interno di un metodo?
Ecco un esempio di quello che sto cercando di fare:
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);Sospettavo che avrei potuto aver bisogno di scrivere esplicitamente "this << Insert", ma questo produce un errore diverso:
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);Ho anche considerato di chiamare esplicitamente Send (non riesce ad aggiungere il metodo) e this:Send (non riesce a risolvere il nome Send).
Ho anche considerato di chiamare direttamente this:Insert (non riesce a risolvere il nome Insert) e Insert (aggiunge con successo il metodo blah, ma non riesce a scandagliare correttamente il meccanismo di chiamata, quindi nessun argomento viene passato nel metodo).
Motivazione:
Lo sto facendo perché sto cercando di creare automaticamente metodi getter e setter per un elenco di proprietà di classe.Idealmente, sarò in grado di fare quanto segue:
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));