Skip to content

What is the meaning of '[..]'? #14

Answered by floitsch
bentxt asked this question in Q&A
Discussion options

You must be logged in to vote

It the "slice" operator of Toit.

When the compiler sees o[x..y] (where both x and y can be omitted), then that expression resolves to the operator [..] --from=x --to=y.

It is used in strings and lists to get subsections of them:

main:
  str := "1234"
  sub := str[1..]  // => "234"
  sub = str[2..3]  // => "3"

  list := [1, 2, 3, 4]
  sub_list := list[..2]  // => [1, 2, 3]

Note that slices are generally implemented as views into the object. This means that a slice of a big string might keep the string alive.
Similarly, a slice of a list is a view into the list. Changing one, affects the other.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by kasperl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants