Re: valid values?
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 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 > 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
Re: valid values?
On 3/3/19 4:09 AM, Fernando Santagata wrote: On Sun, Mar 3, 2019 at 11:41 AM ToddAndMargo via perl6-users mailto:perl6-users@perl.org>> wrote: That way I can catch bad values at compile time and not have to wait and see what it gets fed. The snippet I showed you doesn't intercepts wrong values at compile time, but rather at run time. -- Fernando Santagata That will work too. Thank you!
Re: valid values?
On Sun, Mar 3, 2019 at 11:41 AM ToddAndMargo via perl6-users < perl6-users@perl.org> wrote: > That way I can catch bad values at compile time and not have > to wait and see what it gets fed. > The snippet I showed you doesn't intercepts wrong values at compile time, but rather at run time. -- Fernando Santagata
Re: valid values?
> On Sun, Mar 3, 2019 at 11:09 AM ToddAndMargo via perl6-users > mailto: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 > On 3/3/19 2:16 AM, Fernando Santagata 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) > -- > Fernando Santagata Hi Fernando, Perfect! Thank you! That way I can catch bad values at compile time and not have to wait and see what it gets fed. In my usage, I will be sending the number of bytes to extract a "little enders" from a Buf. -T
Re: valid values?
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
valid values?
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. ~~~