ceylon/ceylon-js — Issue #33: Remaining issues with nullsafe invoke
Author: ikasiuk
Created: 2012-01-29 20:14:38+00:00
State: open
Labels: bug

We have a working implementation of nullsafe invoke now, see #22. But there are still some issues with this which we should address:

##### Method reference (capture style)

This code:

```
Callable? f2 = obj?.f;
```

generates the following JS in capture style:

```
var $f2=function($){return $===null?$$$cl15.nullsafe:$.f}(getObj());
```

So if `obj` is `null`, `f2` will be assigned `nullsafe` instead of `null`. The nullsafe member operator should return `nullsafe` only if it occurs in the context of an invocation expression, otherwise it should return `null` instead.

##### Method reference (prototype style)

The same Ceylon code generates the following JS in prototype style:

```
var $f2=function(){var $=getObj();return $.f.apply($,arguments)};
```

Here the `null` check is missing completely.

##### Simple call statement

This line:

```
obj?.f();
```

generates the following line in JS:

```
function($){return $===null?$$$cl15.nullsafe:$.f}(getObj())();
```

Unfortunately this appears to be not a valid JS statement. node.js complains about an unexpected token at the first `(`.

##### Lost `this` in prototype style

For the following code:

```
class C() {
String s = "hi";
shared String f() {return s;}
}
...
C? obj = C();
String? s = obj?.f();
```

the last line becomes:

```
var $s=function($){return $===null?$$$cl15.nullsafe:$.f}(getObj())();
```

That looks ok, but doesn't work in prototype style: `TypeError: Object #