Skip to content

Add //= and ||= #10

@schwern

Description

@schwern

Defaults are normally applied when the argument isn't passed in at all. This leaves the problem of what to do when an arg is undefined or false.

fun hello($place="World") { print "Hello, $place!\n" }
hello();  # "Hello, $place!
hello(undef);  # "Hello, !"

To fix that you can't have the default in the parameter list, you're back to doing it in the body. Any time the user has to revert to doing parameter parsing in the body that defeats the point of using parameter lists. It's a sign the parameter syntax isn't expressive enough.

fun hello($place) { $place //= "World";  print "Hello, $place!\n" }

Perl programmers do some wacky things with function arguments, but applying a default if the value is undefined or false is pretty common. Perl already has syntax for that, //= and ||=. So just support that!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions