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

标签 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

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

最佳答案

在 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/

相关文章:

iolanguage - Io 语言基础知识

ubuntu-14.04 - 让 Io 语言在 Ubuntu 14.04 上运行

iolanguage - Io : How to instantiate a subclassed primitive (e. g。数字)?

operators - Io 运算符,似乎无法在文件中创建它们

serialization - 如何反序列化 Io 中的对象?

iolanguage getSlot 未返回预期对象

iolanguage - io 赋值运算符不评估?

concurrency - Io 语言如何自动检测死锁?