class Junction

Logical superposition of values

class Junction is Mu { }

A junction is an unordered composite value of zero or more values. Junctions autothread over many operations, which means that the operation is carried out for each junction element (also known as eigenstate), and the result is junction of the return values of all those operators.

Junctions collapse into a single value in boolean context. The semantics depend on the junction type, which can be all, any, one or none.

type constructor operator True if ...
all all & no value evaluates to False
any any | at least one value evaluates to True
one one ^ exactly one value evaluates to True
none none no value evaluates to True

Autothreading happens when a junction is bound to a parameter of a code object that doesn't accept values of type Junction. Instead of producing an error, the signature binding is repeated for each value of the junction.


my $j = 1|2;
if 3 == $j + 1 {
    say 'yes';

First autothreads over the infix:<+> operator, producing the Junction 2|3. The next autothreading step is over infix:<==> , which produces False|True. The if conditional evaluates the junction in boolean context, which collapses it to True. So the code prints yes\n.

Note that the compiler is allowed to parallelize and short-circuit autothreading (and Junction behavior in general), so it is usually an error to autothread junctions over code with side effects.

Junctions are meant to be used as matchers in boolean context; introspection of junctions is not supported. If you feel the urge to introspect a junction, use a Set or a related type instead.

Usage examples:

my @bool_or_int = grep Bool|Int, @list;

sub is_prime(Int $x) returns Bool {
    # 'so' is for boolean context
    so $x %% none(2..$x.sqrt);
my @primes_ending_in_1 = grep &is_prime & / 1$ /, 2..100;
say @primes_ending_in_1;        # 11 31 41 61 71

Special care should be taken when using all with arguments that may produce an empty list:

my @a = ();
so all(@a) # True, because there are 0 False's

To express "all, but at least one", you can use @a && all(@a) say so @a && all(@a); # False Negated operators are special-cased when it comes to autothreading. C<$a !op $b> is rewritten internally as C<!($a op $b)>. The outer negation collapses any junctions, so the return value always a plain L<Bool>. my $word = 'yes'; my @negations = <no none never>; if $word !eq any @negations { say '"yes" is not a negation'; } Note that without this special-casing, an expression like C<$word ne any @words> would always evaluate to C<True> for non-trivial lists on one side. For this purpose, C<< infix:<ne> > counts as a negation of infix:<eq> .

In general it is more readable to use a positive comparison operator and a negated junction:

my $word = 'yes';
my @negations = <no none never>;
if $word eq none @negations {
    say '"yes" is not a negation';

See Also

Type graph

Below you should see a clickable image showing the type relations for Junction that links to the documentation pages for the related types. If not, try the PNG version instead.

perl6-type-graph Junction Junction Mu Mu Junction->Mu

Methods supplied by class Mu

Junction inherits from class Mu, which provides the following methods:

method Str

multi method Str()   returns Str

Returns a string representation of the invocant, intended to be machine readable.

method clone

method clone(*%twiddles)

Creates a shallow clone of the invocant. If named arguments are passed to it, their values are used in every place where an attribute name matches the name of a named argument.

method new

multi method new(*%attrinit)

Default method for constructing (create + initialize) new objects of a class. This method expects only named arguments which are then used to initialize attributes with accessors of the same name.

Classes may provide their own new method to override this default.

method bless

method bless(*%attrinit) returns Mu:D

Lower-level object construction method than new.

Creates a new object of the same type as the invocant, uses the named arguments to initialize attributes, and returns the created object.

You can use this method when writing custom constructors:

class Point {
    has $.x;
    has $.y;
    multi method new($x, $y) {
        self.bless(:$x, :$y);
my $p =, 1);

(Though each time you write a custom constructor, remember that it makes subclassing harder).

method CREATE

method CREATE() returns Mu:D

Allocates a new object of the same type as the invocant, without initializing any attributes.

method print

multi method print() returns Bool:D

Prints value to $*OUT after stringification using .Str method without newline at end.

method say

multi method say() returns Bool:D

Prints value to $*OUT after stringification using .gist method with newline at end.

method ACCEPTS

multi method ACCEPTS(Mu:U: $other)

Performs a type check. Returns True if $other conforms to the invocant (which is always a type object or failure).

This is the method that is triggered on smart-matching against type objects, for example in if $var ~~ Int { ... }.

method WHICH

multi method WHICH() returns ObjAt:D

Returns an object of type ObjAt which uniquely identifies the object. Value types override this method which makes sure that two equivalent objects return the same return value from WHICH.

method WHERE

method WHERE() returns Int

Returns an Int representing the memory address of the object.

method WHY

multi method WHY()

Returns the attached Pod value. For instance,

    sub cast(Spell $s)
    #= Initiate a specified spell normally
    #= (do not use for class 7 spells)
    say &cast.WHY;


Initiate a specified spell normally (do not use for class 7 spells)

See the documentation specification for details about attaching Pod to variables, classes, functions, methods, etc.

This documentation was generated from Junction.pod.