Skip to content

Provide an easy way to specify argument values if argument is not defined, rather than absent #469

Open
@lizmat

Description

@lizmat

Inspired by Paweł bbkr Pabian's comment on https://dev.to/lizmat/quicker-to-assume-2cpj

We currently have a way to specify a default value if an argument in a signature is not specified:

-> $a = 42 { say $a }

However, we do not have an easy way to specify a default value if the argument is specified, but contains an undefined value (a value that returns False on .defined).

I propose the following syntax for such a case:

-> $a //= 42 { say $a }

This would assign the value 42 to $a if the value returns False for .defined. This would also be the case if the argument was not specified, as the default for the container is Any, which is not defined.

This syntax is currently a compilation error:

$ raku -e '-> $a //= 42 { $a }'
===SORRY!=== Error while compiling -e
Malformed parameter
at -e:1
------> -> $a⏏ //= 42 { $a }

One example use case in the core:

    method hyper(
      Int(Cool) :$batch,
      Int(Cool) :$degree,
    ) {
        HyperSeq.new:
          configuration =>
            HyperConfiguration.new(
              :batch($batch // 64),
              :degree($degree // Kernel.cpu-cores-but-one),
              :method<hyper>
            ),

could then be written as:

method hyper(
      Int(Cool) :$batch //= 64,
      Int(Cool) :$degree //= Kernel.cpu-cores-but-one,
    ) {
        HyperSeq.new:
          configuration =>
            HyperConfiguration.new(:$batch, :$degree, :method<hyper>)

Metadata

Metadata

Assignees

No one assigned

    Labels

    languageChanges to the Raku Programming Language

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions