Skip to content

Commit

Permalink
Add: Signature/Attribute Defaults Based on Definedness
Browse files Browse the repository at this point in the history
  • Loading branch information
zoffixznet committed Aug 9, 2016
1 parent 899560e commit ce29255
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion v6d.pod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Plans for Perl 6 version 6.d
Created: 09 Aug 2016

Last Modified: 09 Aug 2016
Version: 1
Version: 2

This documents contains planned changes for v6.d, and who wants to do them.

Expand Down Expand Up @@ -70,4 +70,61 @@ Examples of this happening have showed up in the past.

Jonathan Worthington


=head1 Signature/Attribute Defaults Based on Definedness

=head2 Current Behaviour

Consider the following two examples:

helper "foo";

sub helper ($str, :$where) { debug-print $str.uc, :$where; }
sub debug-print ($str, :$where = 'out') { ... }

########

class Foo {
has $.x = 42;
submethod BUILD (:$!x) {}
}

If we don't specify a C<:where> argument when calling C<helper> or the
C<:x> argument when initializing class C<Foo>, the specified defaults won't
be applied, despite the values not being there, from the programmer's point of
view.

This means all of the attribute defaults have to be moved to the C<BUILD>
submethod and the solution for routines is to do (or to work with Captures,
which hides the passed arguments):

sub debug-print ($str, :$where is copy ) {
$where //= 'out';
...
}

=head2 New Behaviour

I propose we add another syntax for assigning defaults:

sub (:$where //= 'out') { ... }
...
has $.x //= 42;

Such defaults would be assigned if the given value is a C<:U> value, even if
it was explicitly specified.

=head2 Rationale

Currently, the defaults are applied only when the value was not specified.
This creates issues when we want to just propagate the arguments we received,
since the mechanism for defaults assumes we gave a value, even though
it's undefined. The additional method to specify defaults will behave closer
to what typical (IMO) defaults require: e.g. C<Int:U> is not a useful value for
a variable that defaults to C<42>.

=head2 Stakeholder

Zoffix Znet

=cut

0 comments on commit ce29255

Please sign in to comment.