2012-10-23 32 views
3

我從7 Languages in 7 Weeks嘗試下面的示例:如何在交互式Io解釋器中輸入多行方法?

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

使用分號作爲一個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

雖然木衛一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

+1

啊好吧,也許我可以提交一個bug報告:)至少它是不someth在我的機器上發生奇怪的事情。 – Jedidja

+0

我似乎記得REPL中的readline/multilines與過去有問題一樣,並且默認情況下經常關閉(因爲偶爾REPL會偶爾死掉)。 – draegtun

相關問題