Monday, June 6, 2011

golang interface is not nil even if 'containing' a nil pointer

Today I was struggling with a weird issue with "err != nil" during a refactoring of the go9p library. Here is a simplified situation:

This prints:

"Test *main.Test 0x0 dummy"

here 'err' is not nil, even if nil is actually returned.

The problem arised because go9p has an inner API returning some custom *Error, which is then 'casted' to a os.Error interface in the public API layer, so that it can implement the io.Reader,... interfaces. All code using this standard API was getting errors even if no error was returned.

From what I understood, it goes this way: *Test implements os.Error. *Test can be nil since it's a pointer.
os.Error is an interface value. Any interface value can also be nil.
When a value of type X is put* in a variable (or return value) declared as interface Y, then a new value is created holding X. In our case a value of type os.Error contains a *Test, both of which can be nil.

"if err != nil" checks whether 'err', the interface value, is nil, which is not.  "%T" fetches the runtime type of err, which skips (confusingly) the interface envelope and prints *main.Test.
"%p" also skips the interface value, probably because it's not really a pointer (or perhaps because it sounded more natural to follow what the underlying real value points to).
Lastly "%s" invokes the String() method, which is implemented for *Test; here "self != nil" is true because here we have the real underlying pointer. 

So, assuming I understood correctly what's happening, we can generalize this issue to:

This is because an interface value has to have a "zero" value, and it's nil. If you assign a pointer which implements that interface, that pointer can point somewhere or be nil, it doesn't matter, it's a valid pointer; and if you assign this pointer to the interface value, that interface value now has a value which is not nil.

I imagine that otherwise it would be impossible to have a zero value for the interfaces:

So, in order to fix this code you have to catch and "rethrow" the error:

This might seem a big issue, but usually this kind of code already checks for errors and returns so you just have to pay attention to not assigning to a previously declared interface value like os.Error, for example here "err" was inferred as os.Error in a previous ":=" assignement and then used to hold a *Error:

Tuesday, January 25, 2011

scala generics and eclipse autocompletion

The Scala 2.8.1 compiler doesn't create correctly the generics signature in some cases

Eclipse 3.6 is very strict about the signatures and it's autocompletion feature throws an exception when you work on a java project using scala code:

The issue is being fixed in both projects, but in the meantime I needed to work in eclipse with the akka library, and this issue is annoying.

I wrote a small tool which tries to fix the scala classes. Currently I tested it only on scala-library.jar, which contained the "orElse" method whose signature affected my setup. I don't know if it fixes all the problems, but I tried it on my project and eclipse  doesn't complain.

For now I only tried to put that jar in the eclipse IDE classpath, while keeping the original scala-library.jar in the executable classpath.

You can get the code at:

Thursday, January 13, 2011


A colleague of mine told me today that that his girlfriend woudln't believe that there was an optical illusion in the animated dancer image that you can find at:

I was just about to play with html5 canvas drawing, and so I wanted to try out to pilot the illusion with a small red ball which can rotate in both directions, following an elliptical path (an illusion of perspective) and appearing to disappear when going "behind" the dancer.

I don't know if it works for all, but for me this simple (and unprecise) animation, combined with the psycologial effect introduced by clicking on a "change direction" button, swaps the dancer's direction immediately in my head, or at least within one rotation.

Click here to se a demo

Source code on github