我正在尝试来自 7 Languages in 7 Weeks 的以下示例:
Object ancestors := method(
prototype := self proto
if(prototype != Object,
writeln("Slots of ", prototype type, "\n---------------")
prototype slotNames foreach(slotName, writeln(slotName))
writeln
prototype ancestors))
如果我将代码与示例的其余部分一起放在一个文件中(例如 animals.io
),并通过命令行 io Animals.io
执行它,那么它按预期工作。
但是,如果我尝试手动键入该方法并为任何对象执行它,则会收到以下错误:
Exception: Object does not respond to 'prototype'
---------
Object prototype Command Line 1
Object ancestors Command Line 1
是否可以通过交互式解释器输入这个多行方法?
最佳答案
在 REPL 中使用分号作为行分隔符。
Object ancestors := method(
prototype := self proto;
if(prototype != Object,
writeln("Slots of ", prototype type, "\n---------------");
prototype slotNames foreach(slotName, writeln(slotName));
writeln;
prototype ancestors))
关于iolanguage - 如何在交互式 Io 解释器中输入多行方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13021801/