1

私は、第 3 章の Day to of Seven Languages in Seven Weeks (「The Sausage King」) に取り組んでいます。本からコードをそのままコピーしましたが、機能しません。

Io 20110905

OperatorTable に新しい演算子を追加します。

Io> OperatorTable addOperator("xor", 11)
==> OperatorTable_0x336040:
Operators
  0   ? @ @@
  1   **
  2   % * /
  3   + -
  4   << >>
  5   < <= > >=
  6   != ==
  7   &
  8   ^
  9   |
  10  && and
  11  or xor ||
  12  ..
  13  %= &= *= += -= /= <<= >>= ^= |=
  14  return

Assign Operators
  ::= newSlot
  :=  setSlot
  =   updateSlot

To add a new operator: OperatorTable addOperator("+", 4) and implement the + message.
To add a new assign operator: OperatorTable addAssignOperator("=", "updateSlot") and implement the updateSlot message.

出力は、それがスロット 11 に追加されたことを確認します。ここで、true にまだ xor メソッドが定義されていないことを確認しましょう。

Io> true slotNames
==> list(not, asString, asSimpleString, type, else, ifFalse, ifTrue, then, or, elseif, clone, justSerialized)

そうではありません。それでは作成してみましょう。

Io> true xor := method(bool if(bool, false, true))
==> method(
    bool if(bool, false, true)
)

また、false の場合も 1 つです。

Io> false xor := method(bool if(bool, true, false))
==> method(
    bool if(bool, true, false)
)

xor 演算子が追加されたことを確認します。

Io> true slotNames
==> list(not, asString, asSimpleString, type, else, xor, ifFalse, ifTrue, then, or, elseif, clone, justSerialized)

偉大な。使えますか?(繰り返しますが、このコードは本から直接引用したものです。)

Io> true xor true

  Exception: true does not respond to 'bool'
  ---------
  true bool                            Command Line 1
  true xor                             Command Line 1

いいえ。「bool に応答しない」の意味がわかりません。

4

1 に答える 1

6

コンマを忘れて、最初のメッセージがbool- であるパラメーターなしのメソッドを定義trueしました。あなたがやりたかったことは

true xor := method(bool, if(bool, false, true))
//                     ^
// or much simpler:
false xor := true xor := method(bool, self != bool)
// or maybe even
false xor := true xor := getSlot("!=")
// or, so that it works on all values:
Object xor := getSlot("!=")
于 2014-12-12T16:16:10.820 に答える