Why do I see strange index behavior on scalars?

It seems that the scalar itself is a list of one element:

> "foo"[1]
Index out of range. Is: 1, should be in 0..0
  in block <unit> at <unknown file> line 5

> "foo"[0]
foo
> "foo"[*-1]
foo

I call the list a list because lists do not seem to have a range of indices:

> (0, 1, 2)[3]
Nil

What's going on here. What I do not understand about the operator [].

+4
source share
2 answers

This is a function Any. From the docs :

Because Perl 6 intentionally mixes items and lists from a single item, most methods are Anyalso present in the class Listand are enforced on Listor on the list type.

, , , Any AT-POS, , 0.

Nil .

, Nil - , . , "" - , , , , .

, Array , Any , -.

+7

,

, , , , .

[] .

( Positional, - , ) "", " ".

.

# Value treated as list    shape   Indexing range

'a scalar value'           (1)     0..0         
(0,1,2)                    (*)     0..Inf
my @a                      (*)     0..Inf
my @b[42]                  (42)    0..41

( (, (0,1,2))), , (*). (3) ( 3- ), , ? (, ?))

+5

Source: https://habr.com/ru/post/1657198/


All Articles