An Array isn't a type of Str.

    @ ~~ Str; # False
    Array ~~ Str; # False

You can have an array that has a type constraint.

    (my Str @) ~~ Array[Str]; # True
    (my @ of Str) ~~ Array[Str]; # True

Or you could check that all of the values of the Array are of some type.

    (my @ = <a b c>) ~~ *.all ~~ Str
    (my @ = <a b c>) ~~ (.all ~~ Str)

This is one of the few times that it is acceptable to have a ~~ in a
smart-match.

    sub test1(:@array? where .all ~~ Str) { say 'ok' }

    test1; # ok
    test1 array => <a b c>; # ok

    test1 array => (1,2,3);
    # Constraint type check failed in binding to parameter '@array'; …

Somewhere on the Internet I layed out the rules that I think that
should normally be followed, but I am not sure where.

On Mon, Mar 4, 2019 at 5:32 AM Fernando Santagata
<nando.santag...@gmail.com> wrote:
>
> Hi Brad,
>
> How far should I follow the rule that I should not use a smartmatch in a 
> where clause?
>
> I'm thinking of this:
>
> > sub test1(:@array? where Str) { say 'ok' }
> &test1
> > test1()
> Constraint type check failed in binding to parameter '@array'; expected 
> anonymous constraint to be met but got Array ($[])
>   in sub test1 at <unknown file> line 1
>   in block <unit> at <unknown file> line 1
>
> > sub test2(:@array? where .all ~~ Str) { say 'ok' }
> &test2
> > test2()
> ok
>
> The where clause in test1() doesn't work, but is the clause in test2() 
> dangerous (action at a distance)? Should I rephrase it differently?
>
> On Mon, Mar 4, 2019 at 5:29 AM Brad Gilbert <b2gi...@gmail.com> wrote:
>>
>> The `where` clause is already a smart-match, adding `~~` to it is not
>> only redundant, it can cause confusing action at a distance.
>> (By that I mean the right side of `where` is exactly the same as the
>> right side of `~~`)
>>
>> You wouldn't write this:
>>
>>     * ~~ (* ~~ 1|2|4|8|16)
>>
>> So don't write this either:
>>
>>     … where * ~~ 1|2|4|8|16
>>
>> ---
>>
>> It should be
>>
>>     sub mysub(Int $value where 1|2|4|8|16)
>>    {
>>       say "Got $value"
>>     }
>>
>> On Sun, Mar 3, 2019 at 4:16 AM Fernando Santagata
>> <nando.santag...@gmail.com> wrote:
>> >
>> > Hi Todd,
>> > is this what you're looking for?
>> >
>> > sub mysub(Int $value where * ~~ 1|2|4|8|16)
>> > {
>> >   say "Got $value"
>> > }
>> >
>> > mysub 2; # Got 2
>> > mysub 3; # Constraint type check failed in binding to parameter '$value'; 
>> > expected anonymous constraint to be met but got Int (3)
>> >
>> > On Sun, Mar 3, 2019 at 11:09 AM ToddAndMargo via perl6-users 
>> > <perl6-users@perl.org> wrote:
>> >>
>> >> Hi All,
>> >>
>> >> I want to pass an integer to a sub.  The only
>> >> valid values of the integer are 1, 2, 4, 8, and 16.
>> >>
>> >> Other than using "if" to test their values, is
>> >> there a way to state that an integer can only
>> >> have certain predefined values?
>> >>
>> >> Many thanks,
>> >> -T
>> >>
>> >> --
>> >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> >> Having been erased,
>> >> The document you're seeking
>> >> Must now be retyped.
>> >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> >
>> >
>> >
>> > --
>> > Fernando Santagata
>
>
>
> --
> Fernando Santagata

Reply via email to