문제

I am trying the following sample from 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))

If I put the code in a file with the rest of the example (e.g. animals.io) and execute it via the command line io animals.io then it works as expected.

However, if I attempt to type the method in manually and execute it for any object, I get the following error:

Exception: Object does not respond to 'prototype'
---------
Object prototype                     Command Line 1
Object ancestors                     Command Line 1

Is it possible to enter this multi-line method via the interactive interpreter?

도움이 되었습니까?

해결책

use semicolon as a line separator in 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))

다른 팁

While the Io REPL allows you to enter multiline statements [1], it unfortunately seems to just concatenate the lines together :(

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] - You will probably need ReadLine installed on your OS

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top