role PositionalBindFailover

Failover for binding an Iterable to a Positional

role PositionalBindFailover { ... }

This role provides an interface by which an Iterable can be coerced into a Positional, so that you can for example write:

sub fifths(@a{        # @a is constraint to Positional 
my $seq := gather {     # a Seq, which is not Positional 
    take $_ for 1..*;
say fifths($seq);       # OUTPUT: «5␤» 

The invocation of fifths in the example above would ordinarily give a type error, because $seq is of type Seq, which doesn't do the Positional interface that the @-sigil implies.

But the signature binder recognizes that Seq does the PositionalBindFailover role, and calls its cache method to coerce it to a List, which does the Positional role.


method cache

method cache(PositionalBindFailover:D: --> List:D)

Returns a List based on the iterator method, and caches it. Subsequent calls to cache always return the same List object.

method list

method list(PositionalBindFailover:D: --> List:D)

Returns a List based on the iterator method without caching it.

method iterator

method iterator(PositionalBindFailover:D:{ ... }

This method stub ensure that a class implementing role PositionalBindFailover provides an iterator method.

Type graph

Type relations for PositionalBindFailover
perl6-type-graph PositionalBindFailover PositionalBindFailover Mu Mu Any Any Any->Mu Cool Cool Cool->Any Iterable Iterable Seq Seq Seq->PositionalBindFailover Seq->Cool Seq->Iterable

Stand-alone image: vector