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

Merged
merged 2 commits into from
Nov 25, 2024

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.

class.c Show resolved Hide resolved
@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.

@leonerd
Copy link
Contributor Author

leonerd commented Nov 24, 2024

Now with a guard against invalid method names, and a test for the non-scalar error message.

The deparse issues remain, but that also applies to existing methods in general, including :reader generated ones. We should look at that later.

@leonerd leonerd merged commit 883bf45 into Perl:blead Nov 25, 2024
33 checks passed
@leonerd leonerd deleted the class-field-writer branch November 25, 2024 00:09
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