ceylon / ceylon-compiler

DEPRECATED
GNU General Public License v2.0
138 stars 36 forks source link

arguments not evaluated on "direct" nullsafe invocations #2386

Open jvasileff opened 8 years ago

jvasileff commented 8 years ago

With the program:

    variable Integer i = 0;

    variable Integer? opt1 = null;
    variable Integer? opt2 = 0;

    print(opt1?.plus(++i)); // <null>
    print(opt2?.plus(++i)); // 1

the nullsafe method operator acts to short-circuit argument evaluation, but my reading of Section 6.8.6 is that opt1?.plus should first be replaced with a null safe function, which is then (always) invoked with the argument ++i.

When we force the issue with a Callable, the results are as expected:

    variable Integer i = 0;

    variable Integer? opt1 = null;
    variable Integer? opt2 = 0;

    value o1p = opt1?.plus;
    value o2p = opt2?.plus;

    print(o1p(++i)); // <null>
    print(o2p(++i)); // 2
jvasileff commented 8 years ago

@gavinking should the spec be adjusted instead? Although, if arguments are short circuited, it would seem results should be too, allowing opt?.plus(1).plus(1) instead of opt?.plus(1)?.plus(1).

gavinking commented 8 years ago

Hrm. That's a good one. The spec is written that way so that opt?.plus is a valid function reference.

But I agree that we don't actually want to evaluate the arg in opt?.plus(arg) if opt is null. Tricky.