块内可以知道它收到的选择器并根据它自行评估吗?

uno*_*nom 2 arguments smalltalk block function-composition

假设我们在代码中有一个块,我们将它分配给一个变量(实例或本地),就像这样.

someName := [ anInstanceVariable doThis. anotherInstanceVariable doThat.] 
Run Code Online (Sandbox Code Playgroud)

从外面我想用这种方式:

someName someMessageTheBlockDoesntImplement: argument.
Run Code Online (Sandbox Code Playgroud)

块是否可以作用于特定选择器someName并拥有anInstanceVariableanotherInstanceVariable执行它并分别返回这些对象?

PS.它会充当各种各样的货运代理.

Bob*_*mec 6

所有Smalltalk方言都允许某种方式来访问当前上下文.在VisualWorks和Pharo中,您使用#thisContext.所以,如果你有一个像...的方法

Object>>someMethod
    ^[thisContext sender inspect] value
Run Code Online (Sandbox Code Playgroud)

...发送评估:'abc'someMethod ...你将获得aMethodContext - > ByteString(Object)>> someMethod

要么...

Object>>someMethod: aBlock
    ^aBlock value
Run Code Online (Sandbox Code Playgroud)

'abc'methMethod:[thisContext sender inspect]

- > ByteString(Object)>> someMethod:

如果你需要看到更多的堆栈,你可以使用像...

| context frames | 
context := thisContext.
frames := OrderedCollection new.
[context isNil or: [context stack isNil]] whileFalse: [
    frames add: context.
    context := context sender].
^frames
Run Code Online (Sandbox Code Playgroud)

在VA Smalltalk,您可以写...

someMethod
    ^[Processor activeProcess currentFrame context inspect] value
Run Code Online (Sandbox Code Playgroud)

...一个BlockContextTemplate - > [] in Object >>#someMethod

查看调试器代码是查看可以使用的其他技术的好方法.