我正在尝试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
是否可以通过交互式解释器输入这种多行方法?
答案 0 :(得分:2)
在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))
答案 1 :(得分:1)
虽然Io REPL允许你输入多行语句[1],但遗憾的是它似乎只是将这些行连接在一起:(
Io> Object getSlot("ancestors")
==> method(
prototype := self proto if(prototype != Object, writeln("Slots of ", prototype type, "\n---------------") prototype slotNames foreach(slotName, writeln(slotName)) writeln prototype ancestors)
)
[1] - 您可能需要在操作系统上安装ReadLine