Re: Force integers in an array?

2018-08-07 Thread ToddAndMargo

On 08/06/2018 04:38 PM, ToddAndMargo wrote:

Hi All,

Is there a way to force all the members of an array
to be integers and to error out is a non-integer
is written to one of its cells?


Many thanks,
-T




On 08/06/2018 01:40 PM, Benji wrote:
> my Int @a = 1, 2, 3;
>
> @a.push: 'a';
>
> # Type check failed in assignment to @a; expected Int but got Str ("a")
>

Thank you!


Re: Force integers in an array?

2018-08-06 Thread ToddAndMargo

On 08/06/2018 01:44 PM, Curt Tilmes wrote:


On Mon, Aug 6, 2018 at 4:40 PM ToddAndMargo > wrote:


Is there a way to force all the members of an array
to be integers and to error out is a non-integer
is written to one of its cells?


Sure, from the examples in the docs: 
https://docs.perl6.org/language/list#Typing


my Int @a = 1, 2, 3; # An Array that contains only Ints
my @b := Array[Int].new(1, 2, 3); 
# Same thing, but the variable is not typed

my @b := Array[Int](1, 2, 3); # Rakudo shortcut for the same code

@a[0] = 42; # fine
@a[0] = "foo"; # error: Type check failed in assignment




Thank you!


Re: Force integers in an array?

2018-08-06 Thread ToddAndMargo

On 08/06/2018 01:42 PM, Brad Gilbert wrote:

my Int @a;
@a[0] = '1'

Type check failed in assignment to @a; expected Int but got Str ("1")
   in block  at  line 1
On Mon, Aug 6, 2018 at 3:39 PM ToddAndMargo  wrote:


Hi All,

Is there a way to force all the members of an array
to be integers and to error out is a non-integer
is written to one of its cells?


Many thanks,
-T


Thank you!


Re: Force integers in an array?

2018-08-06 Thread Curt Tilmes
On Mon, Aug 6, 2018 at 4:40 PM ToddAndMargo  wrote:

> Is there a way to force all the members of an array
> to be integers and to error out is a non-integer
> is written to one of its cells?
>

Sure, from the examples in the docs:
https://docs.perl6.org/language/list#Typing

my Int @a = 1, 2, 3;  # An Array that contains only Ints
my @b := Array[Int].new(1, 2, 3); # Same thing, but the variable is not typed
my @b := Array[Int](1, 2, 3); # Rakudo shortcut for the same code

@a[0] = 42;   # fine
@a[0] = "foo";# error: Type check failed in assignment


Re: Force integers in an array?

2018-08-06 Thread Brad Gilbert
> my Int @a;
> @a[0] = '1'
Type check failed in assignment to @a; expected Int but got Str ("1")
  in block  at  line 1
On Mon, Aug 6, 2018 at 3:39 PM ToddAndMargo  wrote:
>
> Hi All,
>
> Is there a way to force all the members of an array
> to be integers and to error out is a non-integer
> is written to one of its cells?
>
>
> Many thanks,
> -T


Force integers in an array?

2018-08-06 Thread ToddAndMargo

Hi All,

Is there a way to force all the members of an array
to be integers and to error out is a non-integer
is written to one of its cells?


Many thanks,
-T