Re: Typed Arrays and for-loops

2019-02-13 Thread yary
This disconnect here, is that Mikkel (OP) wants the Str type constraint for
the file list. Which still works with the @ sigil-

> my Str @more_files; @more_files.push('one.file','two.file');
[one.file two.file]

> @more_files.push(99)
Type check failed in assignment to @more_files; expected Str but got Int
(99)

> .say for @more_files # concise, though doesn't show the Str-ness
one.file
two.file

> for @more_files -> Str $f { $f.say }
one.file
two.file

-y

-y


On Wed, Feb 13, 2019 at 5:59 AM Brad Gilbert  wrote:

> The problem with `$files` is the `$`
>
> When a `for` loop sees something that is itemized / containerized
> (`$`) it treats it as a singular value.
>
> my @a = 1,2,3;
> my $a = @a;
>
> for @a { .say }
> # 1
> # 2
> # 3
>
> for $a { .say }
> # [1 2 3]
>
> for @a.item { .say }
> # [1 2 3]
>
> To get it to not see the `$` you have to do something
>
> for $a.list {…}
> for @$a {…}
>
> for $a<> {…} # decontainerize
> for $a.sefl {…} # ditto
>
> On Wed, Feb 13, 2019 at 6:30 AM Mikkel  wrote:
> >
> > Hello. (recurring noob-question here)
> >
> > I seem always seem to get myself confused with Arrays/Typed Arrays. Can
> I ask of you to explain why the 'for' loop does not work without .flat? It
> seems so counter intuitive
> >
> > > my Array[Str] $files;
> > (Array[Str])
> >
> > > $files.push("Test");
> > [Test]
> >
> > > $files.push("Test 2");
> > [Test Test 2]
> >
> > > for $files -> Str $f {.say}
> > Type check failed in binding to parameter '$f'; expected Str but got
> Array[Str] (Array[Str].new("Test", "Test 2"))
> >   in block  at  line 1
> >
> > Best regards
> > Mikkel Birkedam
>


Re: Typed Arrays and for-loops

2019-02-13 Thread Brad Gilbert
The problem with `$files` is the `$`

When a `for` loop sees something that is itemized / containerized
(`$`) it treats it as a singular value.

my @a = 1,2,3;
my $a = @a;

for @a { .say }
# 1
# 2
# 3

for $a { .say }
# [1 2 3]

for @a.item { .say }
# [1 2 3]

To get it to not see the `$` you have to do something

for $a.list {…}
for @$a {…}

for $a<> {…} # decontainerize
for $a.sefl {…} # ditto

On Wed, Feb 13, 2019 at 6:30 AM Mikkel  wrote:
>
> Hello. (recurring noob-question here)
>
> I seem always seem to get myself confused with Arrays/Typed Arrays. Can I ask 
> of you to explain why the 'for' loop does not work without .flat? It seems so 
> counter intuitive
>
> > my Array[Str] $files;
> (Array[Str])
>
> > $files.push("Test");
> [Test]
>
> > $files.push("Test 2");
> [Test Test 2]
>
> > for $files -> Str $f {.say}
> Type check failed in binding to parameter '$f'; expected Str but got 
> Array[Str] (Array[Str].new("Test", "Test 2"))
>   in block  at  line 1
>
> Best regards
> Mikkel Birkedam


Re: Typed Arrays and for-loops

2019-02-13 Thread Timo Paulssen
You could use the @ sigil for your files variable, too. Then you can
just "for @files". There's two ways to get that to work:

my Str @files = "Test", "Test 2";

or

my @files := Array[Str].new("Test", "Test 2");

Hope to help
  - Timo

On 13/02/2019 13:29, Mikkel wrote:
> Hello. (recurring noob-question here)
>
> I seem always seem to get myself confused with Arrays/Typed Arrays.
> Can I ask of you to explain why the 'for' loop does not work without
> .flat? It seems so counter intuitive
>
> > my Array[Str] $files;
> (Array[Str])
>
> > $files.push("Test");
> [Test]
>
> > $files.push("Test 2");
> [Test Test 2]
>
> > for $files -> Str $f {.say}
> Type check failed in binding to parameter '$f'; expected Str but got
> Array[Str] (Array[Str].new("Test", "Test 2"))
>   in block  at  line 1
>
> Best regards
> Mikkel Birkedam


Re: Typed Arrays and for-loops

2019-02-13 Thread Curt Tilmes
On Wed, Feb 13, 2019 at 7:30 AM Mikkel  wrote:

>
> > for $files -> Str $f {.say}
> Type check failed in binding to parameter '$f'; expected Str but got
> Array[Str] (Array[Str].new("Test", "Test 2"))
>   in block  at  line 1
>

Try either 'for @$files'  or ' for $files.list'.


Typed Arrays and for-loops

2019-02-13 Thread Mikkel
Hello. (recurring noob-question here)

I seem always seem to get myself confused with Arrays/Typed Arrays. Can I
ask of you to explain why the 'for' loop does not work without .flat? It
seems so counter intuitive

> my Array[Str] $files;
(Array[Str])

> $files.push("Test");
[Test]

> $files.push("Test 2");
[Test Test 2]

> for $files -> Str $f {.say}
Type check failed in binding to parameter '$f'; expected Str but got
Array[Str] (Array[Str].new("Test", "Test 2"))
  in block  at  line 1

Best regards
Mikkel Birkedam