Why do + and ~ affect Perl 6 junctions differently?

Viewed 241

Add one to a junction of Ints:

put any( 1, 3, 7 ) + 1;

Now you have a junction of those Ints increased by one:

any(2, 4, 8)

So, 2 == any(2, 4, 8) is true.

Make a junction of strings and append to those strings:

put any( <h H> ) ~ 'amadryas';

You get a different result that doesn't equal 'hamadryas' or 'Hamadryas':

any("h", "H")amadryas

I expected something like:

any( 'hamadryas', 'Hamadryas' );

What's the difference in these operations that gives them different behavior even though they should be similar?

2 Answers

Quoting the filed bug report, as progressed by Zoffix++:

Thank you for the report. lizmat++ fixed this.

The put routine does not explicitly handle Junction arguments. As per design, the end result is therefore a call to put for each of its elements:

put any( 1, 3, 7 ) + 1;        # 2␤4␤8
put any( <h H> ) ~ 'amadryas'; # hamadryas␤Hamadryas

Per design, the call order of the puts is indeterminate. So other runs of the same code, perhaps with later compilers, may result in:

put any( 1, 3, 7 ) + 1;        # 4␤8␤2
put any( <h H> ) ~ 'amadryas'; # Hamadryas␤hamadryas

In contrast to put, the say routine does special case Junctions. So the end result is just a single call to say:

say any( 1, 3, 7 ) + 1;        # any(2, 4, 8)
say any( <h H> ) ~ 'amadryas'; # any(hamadryas, Hamadryas)
Related