メソッド内からクラスのインスタンスにメッセージを渡すことはできますか?
これが私がやろうとしていることの例です:
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);「this << Insert」を明示的に記述する必要があるのではないかと考えましたが、別のエラーが発生します。
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);また、Send (メソッドの追加に失敗) と this:Send (Send という名前の解決に失敗) を明示的に呼び出すことも検討しました。
また、これを直接呼び出すことも検討しました。Insert (Insert という名前の解決に失敗します) と Insert (何とかメソッドの追加には成功しますが、呼び出しメカニズムを正しく実装できないため、メソッドに引数が渡されません)。
モチベーション:
これを行うのは、クラス プロパティのリストのゲッター メソッドとセッター メソッドを自動的に作成しようとしているためです。理想的には、次のことができるようになります。
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));