develooper Front page | perl.perl6.users | Postings from May 2019

Re: valid values?

Thread Previous | Thread Next
From:
yary
Date:
May 12, 2019 22:55
Subject:
Re: valid values?
Message ID:
CAG2CFAbWUVGnu23no2M+Z5GRESLm2azbppQXMv9V9oO-BA6V2Q@mail.gmail.com
Side note, using the angle brackets creates IntStr objects, which in some
cases is harmless, sometimes necessary.

> say <1 2 4 8 16 >.perl

(IntStr.new(1, "1"), IntStr.new(2, "2"), IntStr.new(4, "4"), IntStr.new(8,
"8"), IntStr.new(16, "16"))

> say (1,2,4,8,16).perl

(1, 2, 4, 8, 16)

That's thanks to allomorphs from quote-word -
https://docs.perl6.org/language/glossary#index-entry-Allomorph* "**Keep in
mind that certain constructs, such as sets, bags, and mixes care about
object identity, and so will not accept an allomorph as equivalent of its
components alone."*

I usually save angle brackets for strings but in this case, MAIN is getting
IntStr, so we have to also have IntStr for the Set operator element-of:

sub MAIN(Int $value where $value ∈ <1 2 4 8 16>) { say "\$value.perl =
",$value.perl }
perl6 main.p6 4
*says $value.perl = IntStr.new(4, "4")*

sub MAIN(Int $value where $value ∈ (1,2,4,8,16)) {say "\$value.perl =
",$value.perl }
perl6 main.p6 4
*says Type check failed in binding to parameter '<anon>'; expected Any but
got Mu (Mu)*
*  in block <unit> at main.p6 line 1*

Which brings up another question- how to coerce $value to Int? These don't
work, even though IntStr has an Int() method.

sub MAIN(Int(IntStr) $value where $value ∈ (1,2,4,8,16)) {say "\$value.perl
= ",$value.perl }
sub MAIN(Int() $value where $value ∈ (1,2,4,8,16)) {say "\$value.perl =
",$value.perl }


-y


On Thu, Apr 18, 2019 at 5:31 PM mimosinnet <mimosinnet@gmail.com> wrote:

> El Sunday, 03 de March del 2019 a les 02:09, ToddAndMargo via perl6-users
> va escriure:
>
> >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?
>
> I like this syntax:
>
> sub MAIN(Int $value where $value ∈ <1 2 4 8 16 >) {
> ....
> }
>
> It is readable and I really like to use the '∈' symbol :D.
>
> Cheers!
>
> --
> (≧∇≦) Mimosinnet (Linux User: #463211)
>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About