@@ -718,9 +718,15 @@ method called on it.
718
718
=head2 method are
719
719
720
720
multi method are(Any:)
721
+ multi method are(Any: Any $type)
721
722
722
- Returns the strictest type (or role) to which B<all> elements of the list
723
- will smartmatch. Returns L<C<Nil>|/type/Nil> on an empty list.
723
+ The argumentless version available as of release 2022.02 of the Rakudo
724
+ compiler. The version with the type argument is in the 6.e language version
725
+ (early implementation exists in Rakudo compiler 2024.05+).
726
+
727
+ If called without arguments, returns the strictest type (or role) to which
728
+ B<all> elements of the list will smartmatch. Returns L<C<Nil>|/type/Nil>
729
+ on an empty list.
724
730
725
731
say (1,2,3).are; # OUTPUT: «(Int)»
726
732
say <a b c>.are; # OUTPUT: «(Str)»
@@ -736,14 +742,29 @@ Scalar values are interpreted as a single element list.
736
742
say Int.are; # OUTPUT: «(Int)»
737
743
738
744
Hashes will be interpreted as a list of pairs, and as such will always
739
- produce the L<C<Pair>|/type/Pair> type object. Use the C<.keys> or C<.values> method
740
- to get the strictest type of the keys or the values of a hash.
745
+ produce the L<C<Pair>|/type/Pair> type object. Use the C<.keys> or
746
+ C<.values> method to get the strictest type of the keys or the values of
747
+ a hash.
741
748
742
749
my %h = a => 42, b => "bar";
743
750
say %h.keys.are; # OUTPUT: «(Str)»
744
751
say %h.values.are; # OUTPUT: «(Cool)»
745
752
746
- Available as of release 2022.02 of the Rakudo compiler.
753
+ If called with a type argument, will check if all types in the invocant
754
+ smartmatch with the given type. If so, returns C<True>. If any of the
755
+ smartmatches fails, returns a C<Failure>
756
+
757
+ say (1,2,3).are(Int); # OUTPUT: «True»
758
+ say <a b c>.are(Str); # OUTPUT: «True»
759
+ say <42 666>.are(Int); # OUTPUT: «True»
760
+ say <42 666>.are(Str); # OUTPUT: «True»
761
+ say (42,666e0).are(Real); # OUTPUT: «True»
762
+ say (42,i).are(Numeric); # OUTPUT: «True»
763
+ say ("a",42,3.14).are(Cool); # OUTPUT: «True»
764
+ say ().are; # OUTPUT: «True»
765
+
766
+ Int.are(Str); # OUTPUT: «Expected 'Str' but got 'Int'»
767
+ (1,2,3).are(Str); # OUTPUT: «Expected 'Str' but got 'Int' in element 0»
747
768
748
769
=head2 method prepend
749
770
0 commit comments