On Thu, 11 Aug 2016 06:14:52 -0700, sml...@gmail.com wrote:
> You're right, that one *is* inconsistent with `True but False`.
> 
> `$bool1 but $bool2` and `$int1 but $int2` should either both override
> the value, or both *not* override the value.


There's actually no inconsistency and the infix:<but> is much less magical:

The infix:<but> operator with the RHS that isn't a role *creates* a role with a 
single method
that is the .^name of the RHS value which returns that value, and uses that 
role to perform the mixin:

    <Zoffix> m: my $s = 0 but class { method ^name ($) { 'warbles' } }.new; say 
$s.warbles
    <camelia> rakudo-moar e0c0ae: OUTPUT«warbles.new␤»

So the results you claim are inconsistent actually are:

    % perl6 -e'my $s = False but True; say $s; say $s.so;'
    True
    True

- `but True` mixes in a role with method called `Bool` that returns `True`
- `say` uses `.gist`, which for Bool objects evaluates the object in Bool 
context (so, calls .Bool), and so we end up with .Bool from our role that gives 
True
- The `.so` method uses `.Bool` under the hood, which we overrode to True
= both outputs match

In this example:

   % perl6 -e'my $s = 0 but True; say $s; say $s.so;'
   0
   True

- Everything is the same as in the previous example, except the original object 
is an Int
- `say` uses `.gist`, which for Int objects calls .Str method, which we did not 
override with our mix in, hence why we still get `0` in the output
= outputs differ

And since we know that .gist on Ints calls .Str, we can predict that mixing in 
a Str object will change the output of `say`:

    <Zoffix> m: my $s = 0 but 'meows'; say $s; say $s.so;
    <camelia> rakudo-moar e0c0ae: OUTPUT«meows␤False␤»

Indeed it did change the output of `say` and since we did not override .Bool 
this time, the `.so` output gives False now.

So then, if we mixin both a Str and a Bool, we'll affect both outputs:

    <Zoffix> m: my $s = (0 but 'meows') but True; say $s; say $s.so;
    <camelia> rakudo-moar e0c0ae: OUTPUT«meows␤True␤»


So as far as I can see, there's no bug or inconsistency. Closing the ticket.

Cheers,
ZZ

Reply via email to