Reputation: 1861
Consider this code:
OperatorTable addOperator(":", 2)
: := method(value,
list(self, value)
)
hash := "key": "value"
hash println
The return should be list(key, value)
, and when using this in the Io REPL that is exactly the return value. When using the interpreter (as in io somefile.io
) the value returned is value
. After some inspection the difference is here:
# In the REPL
OperatorTable addOperator(":", 2)
message("k" : "v") # => "k" :("v")
# Via the Interpreter
OperatorTable addOperator(":", 2)
message("k" : "v") # => "k" : "v"
Why is this happening?
Upvotes: 2
Views: 174
Reputation: 842
File execution happens in these stages:
So operator to message conversion only happens when the file is initially loaded in stage 2. When the operator registration code is executed in stage 3. this has already happened, thus the operator has no effect.
You can set the order which files get loaded manually and put the operator definition in the first file loaded.
Having a file called operators.io
for example which includes all operator definitions loaded before the files that use them.
Upvotes: 4
Reputation: 6577
After confirming with ticking I arrived at the following solution:
main.io:
doFile("ops.io")
doFile("script.io")
ops.io:
OperatorTable addOperator(":", 2)
: := method(value,
list(self, value))
script.io:
hash := "key": "value"
hash println
Like ticking explains, the whole file is loaded at once so you have to split it up so the loading order guarantees that the operators are available.
Upvotes: 2