Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl 6 '^=' operator doesn't produce a result, causing program to never terminate

Tags:

raku

When using the ^= operator, the program never terminates and it doesn't produce a result. Am I doing something wrong?

To check that I could reproduce this I used this working, verifiable example:

my $k = 1;
$k ^= 24;
say $k;

As well as this, I even tried doing: $k = $k ^ 24; - but this still produces the same issue.

like image 699
madcrazydrumma Avatar asked Jul 15 '18 08:07

madcrazydrumma


1 Answers

There are several infix operators with ^ in them, all of them mean exclusive-or in some form.

  • ^ Junctive xor, which is trueish if exactly one value is trueish. It is a value that can be passed around.

    so     1 == 1 ^ 2; # True
    so     2 == 1 ^ 2; # True
    so    42 == 1 ^ 2; # False
    so 1 ^ 2 == 1 ^ 2; # False
    
    my $v = 1 ^ 2;
    so 1 == $v; # True
    

    I added so to collapse the junction, which you should do as soon as practical.

  • ^^ Short circuiting xor. Returns the only truish value. If there is no truish value it returns the last value. If there is more than one trueish value it short circuits and returns Nil

    say ( 0 ^^ 42 ^^ Nil );
    42
    
    say ( !say(1) ^^ !say(2) ^^ -1 but False );
    1
    2
    -1
    
    say ( say(1) ^^ say(2) ^^ say(3) );
    1
    2
    Nil
    
  • +^ Int bitwise xor. It compares the bits of two Ints and the value it returns has a 1 if exactly one of the Ints has a 1 in that position

    say (0b10101010 +^ 0b10100000).base: 2; # 1010
    
    say 1.5 +^ 2; # 3
    
  • ~^ String bitwise xor. Does the same as +^ except on strings.

    say 'aa' ~^ 'US'; # 42
    

Perl 5 used ^ for both +^ and ~^ depending on what values it was given. Newer versions have separate operators for those if you opt-into using them.


The reason your code doesn't halt is that a Junction created with ^ keeps track of where the value came from.

my $k = 1;
my $v = $k ^ 24;
say $v; # one(1, 24);

$k = 2;
say $v; # one(2, 24);

So then $k ^= 24 creates a self-referential value. Which is fine, until you try to use, or print it.

like image 108
Brad Gilbert Avatar answered Oct 12 '22 14:10

Brad Gilbert