views:

93

answers:

1

I'm trying to define my own operator in Io, and I'm having a hard time. I have an object:

MyObject := Object clone do(
  lst := list()
  !! := method(n, lst at(n))
)

But when I call it, like this:

x := MyObject clone do(lst appendSeq(list(1, 2, 3)))
x !! 2

But I get an exception that argument 0 to at must not be nil. How can I fix?

+5  A: 

Well, there's one problem in your code. Simply put, you haven't added !! to the operator table. I'll give you a little background on this.

Operators in Io are shuffled before the AST is constructed. This means, that we have to maintain a list of known operators with certain precedence levels to know which ones bind tighter than others. We do this in the "OperatorTable". If you are in the REPL, you can see how to use it by typing "OperatorTable" into the REPL (without the quotes). This will give you a list of the operators, (generated dynamically, so new operators are added as they are defined), along with examples of how to use each type of operator. There are two types:

  1. Binary operators (1 + 2 for instance, called simply "operators")
  2. Assignment operators (a := b for instance)

So in your example, your code is right; we don't have to change anything there. However, we are missing one bit of code to make the parsing subsystem know how to handle your operator. I'll provide an example that assumes you want it to bind as tight as multiplication.

OperatorTable addOperator("!!", 3)

Now, we can see how this gets shuffled by constructing a message and looking at how its tree is represented. Again in the REPL, if we type:

message(a !! b)

We will see something like this:

==> a !!(b)

This is just like any other method call, it has to exist somewhere or else you will get an error. You can use it like it is shown above (with explicit parenthesis), or you can use it how you want to in your original question, without explicit parenthesis. As with any operator, you are subject to the rules of precedence if you do not use explicit parenthesis, just so you are aware.

Hope this answers your question.

jer
Thanks for your answer, helpful to know. However, I don't understand why "n" is nil in my !! method.
Alright, well simply put, in Io, only CFunctions (that is, methods that are implemented in C code which fetch specific arguments at specific indexes) have the requirement that you must supply at least N arguments. When an operator isn't known, it isn't shuffled, meaning your original x !! 2 was seen as: message(x !! 2) and not message(x !!(2)). Since !! is defined in Io code with default argument handling, what occurs is that any arguments not explicitly provided are filled in with "nil". That is, the arguments get defined, but they point at nil. This is the source of your problem.
jer