Course of Raku / Advanced / Containers

Contexts 🆕

A value in Raku can sometimes behave differently as it adapts to the context in which it is used. For example, the same array can act as a number (its length), as a string (its elements joined), or as a truth value (array’s empty or not), depending on what the surrounding code asks for.

Understanding context explains a lot of Raku that would otherwise look surprising: why +@array is a number, why an array put in a scalar behaves as a single item, and why if @array tests emptiness. This section covers the main contexts.

Topics in this section

Practice

Complete the quizzes that cover the contents of this section.

Exercises

This section contains 3 exercises. Examine all the topics of this section before doing the coding practice.

  1. Force a number
  2. A Boolean check
  3. Item or list

Course navigation

Solution: Bind a scalar   |   Numeric, string, and Boolean context