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.
Example:
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
Negated operators are special-cased when it comes to autothreading. $a !op $b is rewritten internally as !($a op $b). The outer negation collapses any junctions, so the return value always a plain 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 $word ne any @words would always evaluate to True for non-trivial lists on one side.
For this purpose, < 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';
}
http://perl6maven.com/perl6-is-a-value-in-a-given-list-of-values
http://perl6advent.wordpress.com/2009/12/13/day-13-junctions/
Full-size type graph image as SVGJunction inherits from class Mu, which provides the following methods:
multi sub defined(Mu) returns Bool:D multi method defined() returns Bool:D
Returns False on the type object, and True otherwise.
multi sub Bool(Mu) returns Bool:D multi method Bool() returns Bool:D
Returns False on the type object, and True otherwise.
multi method Str() returns Str
Returns a string representation of the invocant, intended to be machine readable.
multi sub gist(Mu) returns Str multi method gist() returns Str
Returns a string representation of the invocant, optimized for fast recognition by humans.
The default gist method in Mu re-dispatches to the perl method, but many built-in classes override it to something more specific.
multi sub perl(Mu) returns Str multi method perl() returns Str
Returns a Perlish representation of the object (i.e., can usually be reparsed to regenerate the object).
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.
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(Mu $candidate, *%attrinit) returns Mu:D
Lower-level object construction method than new.
If you pass a Whatever as a candidate, it creates a new object of the same type as the invocant, and then uses the named arguments to initialize attributes.
If you pass something other than a Whatever object as a candidate, it simply does the attribute initialization on the $candidate.
In both cases, the object with the attributes initialized is returned.
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 = Point.new(-1, 1);
(Though each time you write a custom constructor, remember that it makes subclassing harder).
method CREATE() returns Mu:D
Allocates a new object of the same type as the invocant, without initializating any attributes.
multi method print() returns Bool:D
Prints value to $*OUT after stringification using .Str method without newline at end.
multi method say() returns Bool:D
Prints value to $*OUT after stringification using .gist method with newline at end.
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 { ... }.
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.