函数中的“死方法上下文”错误

时间:2019-05-11 02:58:53

标签: function smalltalk

我正在尝试编写一个isBinary函数,以检查发送的行中是否有任何不可打印的字符(整数值在0-127范围之外):

isBinary := [ :sline |
    'Reached isBinary fn.' displayNl.
    sline do: [ :char |           "for each character"
        i := char asInteger.      "convert to integer"
        (i < 0 | i > 127) 
        ifTrue: [^true]. ].       "return true if found unprintable"
    ^false. ].                    "if not found above, return false"

(Directory working: '.') allFilesMatching: '*.x'
do: [ :ff |
    ((ff name), ' : ') display.
    infile := FileStream open: ff name mode: FileStream read.
        firstline := infile nextLine.
        (isBinary value: firstline) 
        ifTrue: ['Binary file' displayNl.]
        ifFalse: [ 'Not a binary file' displayNl].
    infile close ].

isBinary函数已实现,但是它给出以下错误(文件是否为二进制文件):

$ gst isbinary.st
"Global garbage collection... done"
/home/abcd/binaryfile.x : Reached isBinary fn.
Object: Character value: 16rC0 error: return from a dead method context
SystemExceptions.BadReturn(Exception)>>signal (ExcHandling.st:254)
SystemExceptions.BadReturn class(Exception class)>>signal (ExcHandling.st:151)
Character(Object)>>badReturnError (Object.st:1389)
String(SequenceableCollection)>>do: (SeqCollect.st:827)
[] in UndefinedObject>>executeStatements (isbinary.st:4)
optimized [] in UndefinedObject>>executeStatements (isbinary.st:16)
[] in Kernel.RecursiveFileWrapper(FilePath)>>filesMatching:do: (FilePath.st:903)
[] in Kernel.RecursiveFileWrapper>>namesDo:prefixLength: (VFS.st:378)
[] in File>>namesDo: (File.st:589)
BlockClosure>>ensure: (BlkClosure.st:268)
File>>namesDo: (File.st:586)
Kernel.RecursiveFileWrapper>>namesDo:prefixLength: (VFS.st:373)
[] in Kernel.RecursiveFileWrapper>>namesDo:prefixLength: (VFS.st:382)
[] in File>>namesDo: (File.st:589)
BlockClosure>>ensure: (BlkClosure.st:268)
File>>namesDo: (File.st:586)
Kernel.RecursiveFileWrapper>>namesDo:prefixLength: (VFS.st:373)
Kernel.RecursiveFileWrapper>>namesDo: (VFS.st:396)
Kernel.RecursiveFileWrapper(FilePath)>>filesMatching:do: (FilePath.st:902)
File(FilePath)>>allFilesMatching:do: (FilePath.st:775)
Directory class>>allFilesMatching:do: (Directory.st:225)
UndefinedObject>>executeStatements (isbinary.st:11)

在我的代码中用sline do:替换sline asArray do:也不起作用(相同的错误)。

问题出在哪里,如何解决?感谢您的帮助。

编辑: 正如答案和评论中所建议的那样,我在类中使用method编写了以下代码,此方法有效。我只想请您发表评论,这是否是正确的方法。

Object subclass: Checker [ 
    isBinary: sline [ 
        'Reached isBinary fn.' displayNl.
        sline do: [ :char |  | i |           "for each character"
            i := char asInteger.             "convert to integer"
            i > 127
            ifTrue: [^true]     "return true if found unprintable"  
        ].       
    ^false. ]      "if no unprintable char found, return false"
].

(Directory working: '.') allFilesMatching: '*.x'
do: [ :ff |
    '------------------------------' displayNl.
    ((ff name), ' : ') displayNl.
    infile := FileStream open: ff name mode: FileStream read.
        firstline := infile nextLine.
        ((Checker new) isBinary: firstline)
        ifTrue: ['Binary file' displayNl.]
        ifFalse: [ 'Not a binary file' displayNl].
    infile close ].

2 个答案:

答案 0 :(得分:4)

您的isBinary变量绑定到一个包含所谓的非本地返回的块,该块无法按预期方式执行。原因是非本地返回的语义是从定义de block(它的词法上下文)的方法返回。如果这种方法不存在或已经返回(换句话说,如果词法上下文不在调用堆栈中),则无法定义执行流应返回的位置。因此是错误。

要解决此问题,只需创建一个方法#isBinary:,该方法接收一个参数sline以及您为该块编写的代码。然后调用该方法,而不是评估该块。可以。

答案 1 :(得分:0)

下面的独立方法/块代码通过创建一个返回变量来工作,如果发现了无法打印的字符,则该变量的值将被循环处理。然后退出循环:

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"

上面的方法没有创建OP(我!)所需的类。

相关问题