Over the weekend I added some tests on 'undef' behaviour
(t/builtins/undef.t):
These behave as expected:
eval_is('undef * 2', undef, 'undef * 2');
eval_is('undef * undef', undef, 'undef * undef');
These don't (at least, according to my expectations...)
eval_is('undef + 1', undef, 'undef + 1', :todo<bug>); # dies
eval_is('1 + undef', undef, '1 + undef', :todo<bug>); # gives 1
eval_is('2 * undef', undef, '2 * undef', :todo<bug>); # gives 0
eval_is('undef xx 2', undef, 'undef xx 2', :todo<bug>); # dies
I was hoping to figure out enough Haskell to think about how to fix
them, but I have given up for the moment :-)
Ade
I would expect these to both equal 1,
see perl5
>perl5 -le "undef $_; ++$_; print"
1
In this case, you're expecting
(undef) + 1
but you're getting
undef(+1)
instead.
This is because 'undef' serves double-duty as both 'undefined value'
and 'prefix op for undefining variables', and the op form is taking
precedence in parsing.
It's worth noting that Perl5 seems to have exactly the same parsing behaviour:
$ perl -e 'print undef + 1'
Warning: Use of "undef" without parentheses is ambiguous at -e line 1.
Can't modify constant item in undef operator at -e line 1, at EOF
Execution of -e aborted due to compilation errors.
I'm not sure whether this behaviour is supposed to be changing.
> eval_is('1 + undef', undef, '1 + undef', :todo<bug>); # gives 1
> eval_is('2 * undef', undef, '2 * undef', :todo<bug>); # gives 0
AFAIK, a vanilla (undef) numifies to 0, so these are already correct.
If you were expecting undef to behave as a 'poisoned' value then I can
understand the confusion.
> eval_is('undef xx 2', undef, 'undef xx 2', :todo<bug>); # dies
Again, you're expecting
(undef) xx 2
but you're getting
undef(xx 2)
instead.
Stuart
You (and Carl) are absolutely right... all these things are behaving as
they should. Cool! :-)
I'll fix the tests later on today.
Thanks,
Ade
> Hi,
>
> Over the weekend I added some tests on 'undef' behaviour
> (t/builtins/undef.t):
>
> These behave as expected:
>
> eval_is('undef * 2', undef, 'undef * 2');
That's not what I'd expect. I'd expect it to return 0 and throw a warning
about numification.
It is. I think we decided to make the value undef, and the function
undefine(). (But these days most values of undef really ought to
be constructed and returned (or thrown) using fail().)
Larry