메서드 내에서 클래스의 인스턴스로 메시지를 전달할 수 있습니까?
다음은 내가하려는 작업의 예입니다.
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라는 이름을 확인하지 못함) 및 삽입(blah 메서드를 성공적으로 추가했지만 호출 메커니즘을 올바르게 연결하지 못하여 메서드에 인수가 전달되지 않음).
동기 부여:
클래스 속성 목록에 대해 getter 및 setter 메서드를 자동으로 생성하려고 하기 때문에 이 작업을 수행하고 있습니다.이상적으로는 다음을 수행할 수 있습니다.
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));