Re: Different behaviour between interactive and not interactive usage

Jeremy Tregunna <[email protected]> Sat, 03 Mar 2012 13:59:15 -0600
Newsgroups gmane.comp.lang.io
Message-ID <[email protected]>
On 2012-03-03, at 10:31 AM, norbertmuellerde wrote:

> Hello,
> 
> I am trying to use Io under Ubuntu 10.04 
> and have a strange behaviour:
> 
> % cat operator.io 
> OperatorTable addOperator (":",1)
> Object : := method (a, self print; a println)
> 1 : 3
> % io operator.io 
> 1nil
> % io < operator.io 
> Io 20110905
> Io> OperatorTable addOperator (":",1)
> ==> OperatorTable_0x8704dc0:
> Operators
> 0 ? @ @@
> 1 ** :
> 2 % * /
> 3 + -
> 4 << >>
> 5 < <= > >=
> 6 != ==
> 7 &
> 8 ^
> 9 |
> 10 && and
> 11 or ||
> 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.
> 
> Io> Object : := method (a, self print; a println)
> ==> method(a, 
> self print; a println
> )
> Io> 1 : 3
> 13
> ==> 3
> Io> 
> % 
> 
> So it seems that the interpreter sees the argument for operator : only in interactive mode. 
> Could that be a bug?
> 
It's not a bug. Here's why. Operator transformation happens at compile time. In the REPL this is each time you hit enter. However, in a file, it's when the file is initially loaded, before any code is evaluated. As such, you'd have to put your operator table modifications in a separate file, which then loads your other file. Inconvenient, but it's the only way to do this.

Regards,

Jeremy Tregunna