Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

class.c: Define a :writer attribute, applicable to scalar fields only #22765

Open
wants to merge 2 commits into
base: blead
Choose a base branch
from

Conversation

leonerd
Copy link
Contributor

@leonerd leonerd commented Nov 19, 2024

Permits automatic creation of writer accessor methods, in addition to the reader ones we already had.


  • This set of changes requires a perldelta entry, and it is included.

{
char sigil = PadnamePV(pn)[0];
if(sigil != '$')
croak("Cannot apply a :writer attribute to a non-scalar field");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No test for this error.

@tonycoz
Copy link
Contributor

tonycoz commented Nov 20, 2024

It makes deparse unhappy:

$ ./perl -Ilib -MO=Deparse,Foo::set_x -Mfeature=class -e 'class Foo { field $x :writer; }'
class is experimental at -e line 1.
field is experimental at -e line 1.
use feature 'class';
{
    package Foo;
    ;
}
unexpected OP_METHSTART at lib/B/Deparse.pm line 1677.
package Foo;
sub set_x {
    XXX;
    die sprintf("Too many arguments for subroutine at %s line %d.\n", (caller)[1, 2]) unless @_ <= 1;
    die sprintf("Too few arguments for subroutine at %s line %d.\n", (caller)[1, 2]) unless @_ >= 1;
    $x = $_[0];
    return $self;
}
-e syntax OK

but that looks like a general deparse problem with methods:

$ ./perl -Ilib -MO=Deparse,Foo::set_x -Mfeature=class -e 'class Foo { field $x; method set_x { $x = $_[0]; } }'
class is experimental at -e line 1.
field is experimental at -e line 1.
method is experimental at -e line 1.
unexpected OP_METHSTART at lib/B/Deparse.pm line 1677.
use feature 'class';
{
    package Foo;
    ;
    sub set_x : method {
        XXX;
        $x = $_[0];
    }
}
-e syntax OK

You asked about identifier validation in #p5p, but:

$ ./perl -Ilib -Mfeature=class -E 'class Foo { field $x :reader :writer(a c); } my $m = "a c"; say Foo->new()->$m("abc")->x'
class is experimental at -e line 1.
field is experimental at -e line 1.
abc

but I expect :reader has the same problem.

Similarly:

$ ./perl -Ilib -Mfeature=class -E 'class Foo { field $x :writer( ); } say "<$_>" for keys %Foo::'
class is experimental at -e line 1.
field is experimental at -e line 1.
<__ANON__>
<new>

(no method defined, but no error reported)

@leonerd
Copy link
Contributor Author

leonerd commented Nov 20, 2024

On grounds that a bunch of these issues also affect the :reader attribute as well, I think I will first go fix them in a separate PR before this, and then come back to this one after that's done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants