“Dead method context” error in a function

此生再无相见时 提交于 2019-12-01 23:10:33

Your isBinary variable is bound to a block that contains a so called non-local return, which cannot be executed the way you intend. The reason is that the semantics for a non-local return is to return from the method that defines de block (it's lexical context). If such a method does not exist or it already returned (in other words if the lexical context is not in the calling stack), there is no way to define where the execution flow should return. Hence the error.

To solve this, just create a method #isBinary: that receives an argument sline with the code you wrote for the block. Then call the method instead of evaluating the block. That will work.

Following standalone method/block code works by creating a return variable whose value is manipulated in loop if unprintable character is found. The loop is then exited:

isBinary := [ :sline |            "WORKS"
    'Reached isBinary fn: ' display.
    ret := false.                 "return variable initialized to false"
    sline do: [ :char |           "loop for each character in sent line"
        i := char asInteger.      "convert to integer"
        i > 127                   "check if printable"
        ifTrue: [ret := true. exit]].   "ret becomes true if found unprintable; does not work if ^ symbol is used"  
    ret].            "if not found above, ret remains false; ret is returned value"

Above works without creating a class as desired by OP (me!).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!