如何在交互式Io解释器中输入多行方法?

时间:2012-10-23 00:06:54

标签: iolanguage

我正在尝试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

是否可以通过交互式解释器输入这种多行方法?

2 个答案:

答案 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