Skip to content

Document when blocks are created implicitly and when they are not created by explicit {...} #3536

Open
@codesections

Description

@codesections

Here's a Raku puzzle: which of the following statements refer to the same block as statement 1 and which create new blocks?

{ dd &?BLOCK;              # 1
  if 1 > 0 { dd &?BLOCK};  # 2
  dd &?BLOCK if 2 > 1;     # 3 
  for (1) { dd &?BLOCK};   # 4
  dd &?BLOCK for (1)}      # 5

Based on the current documentation and my understanding that {...} creates a new block, I strongly expected 1, 3, and 5 to refer to the same block and 2 and 4 to refer to new blocks. However, this is not the case.

Instead, 1, 2, and 3 refer to the same block, while 4 and 5 create a new block.

However, the same rules that apply to creating blocks for the purpose of &?BLOCK do not apply to creating lexical scope blocks:

{ my $a = 1; 
   if 1 > 0 { 
      my $a = 2; 
      say $a;          # OUTPUT «2»
      say $OUTER::a    # OUTPUT «1»
   }; 
 say $a                # OUTPUT «1»
}

The existing docs do not distinguish between lexical scope and blocks, but apparently the two concepts are distinct at least in this case. The docs should make a clearer distinction here.

Suggestions

I am not sure when blocks are created and when they are not, but the documentation should be updated to reflect that blocks are not created whenever {...} appears. Is the rule that blocks are created whenever the topic variable is changed?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugError of some kind, from a typo to errors in the examples.docsDocumentation issue (primary issue type)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions