我正在尝试编写一个返回新值的方法。以下代码是从这里修改的:
| stripChars |
stripChars := [ :string :chars |
str := string reject: [ :c | chars includes: c ].
str displayNl. "THIS WORKS."
^ str "THIS DOES NOT WORK."
].
newstr := stripChars
value: 'She was a soul stripper. She took my heart!'
value: 'aei'.
newstr displayNl.
虽然上面的函数创建了新的字符串并显示出来,但是在返回或接收返回的新字符串时会出错:
$ gst make_fn_ques.st
Sh ws soul strppr. Sh took my hrt!
Object: 'Sh ws soul strppr. Sh took my hrt!' error: return from a dead method context
SystemExceptions.BadReturn(Exception)>>signal (ExcHandling.st:254)
SystemExceptions.BadReturn class(Exception class)>>signal (ExcHandling.st:151)
String(Object)>>badReturnError (Object.st:1389)
UndefinedObject>>executeStatements (make_fn_ques.st:10)
nil
问题出在哪里,如何解决?谢谢你的帮助。
这
^ str
不从块(stripChars)返回,而是从封闭方法返回(非本地返回)。
显然 GNU Smalltalk 不允许您从以这种方式传递给 gst 的脚本返回。
只需删除 ^,并仅保留str
作为块的最后一个表达式。这将导致str
成为块的返回值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句