Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

RFC: Concise OOP Syntax

by LanX (Sage)
on Aug 05, 2019 at 00:59 UTC ( #11103898=perlmeditation: print w/replies, xml ) Need Help??

Dear monastery!

In continuation to

I tried to hack a proof of concept for a concise OO syntax

The basic ideas are that:

Declaration of instance variable
  • my on the class level with :attributes
  • the TYPE is given right after the my
  • attributes reflect the Moo(se-)model of has keys where possible
  • the assigned values are defaults for the new-constructor
Access of instance variables inside methods
  • an instance variable x is readable and writable via $$x
  • this is automatically mirrored in $self->{x}
  • $self->{x} is an alternative syntax for the same access
  • $self is already shifted from @_ and directly available
  • all subs declared inside the scope of a class are methods
  • imported subs (like pp) are ignored

{ use Class BLA => ISA-LIST; use Data::Dump qw/pp/; my Int ($x,$y) :has :rw = (10,11); sub set_x { my ($var) = @_; #warn "set_x $$x -> $var \n"; $$x = $var; } sub print_x { print "x = $$x \n"; } sub print_self_x { print "x = $self->{x} (via \$self)\n"; } sub dump { warn '$self:',\$self; warn pp '$self: ', $self; warn pp '$$x: ', $$x; warn pp '$$y: ', $$y; } }

The implementation is done via a macro expansion from use Class which injects some boilerplate into the head of the class, which handles the creation.

Injecting is basically done via a source filter or alternatively via Keyword::Simple. NB: just injecting some code doing introspection. No parsing, regexing or modification of the code you see.

I'm supposing this concise syntax could be used as a front end for all current OO models in Perl and might help offering a stable backwards compatible syntax if it's hardcoded into the engine.

A rough proof of concept follows here:

NB: This example is pretty barebone, and not meant to be an alternative to other OO Frameworks, but rather a frontend. It doesn't create accessors and the constructor is only simplistic.


Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re: RFC: Concise OOP Syntax
by daxim (Curate) on Aug 05, 2019 at 09:08 UTC
    As I've told you several times, your assumptions [m]ost of us are stuck with an older version of Perl and [t]he crucial problem for the acceptance is backwards compatibility are just not true. (I don't know the name of the logical fallacy, but essentially you project your own position and circumstances onto everyone.) You burden yourself with a lot of work that is based on false premises and if finished is realistically useful only to a minuscule amount of users.

    The rest can (and could for years) install Moops and get to enjoy superb class/roles/methods/types syntax sugar.

    use Moops; use Data::Dx; class BLA extends SOME::PARENT with SOME::ROLE, ANOTHER::ROLE { has x => is => 'rw', isa => Int, writer => 'set_x', default => 10; has y => is => 'rw', isa => Int, writer => 'set_y', default => 11; method print_x() { printf "x = %s\n", $self->x; } method dump() { Dx $self; } } my $bla = BLA->new; $bla->set_x(42); $bla->print_x; $bla->dump;
    If I had your skill and internals knowledge, I would improve Moops to add sugar for self-less attributes and less verbose attribute declarations.
      And I told you several times that I disagree.

      Thanks for the link mention of Moops, but it doesn't look like a syntax which will ever get into core.


      > most of us are stuck with an older version

      never said this.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice


      sigh ...

      > STATUS

      > Unstable.

      > Until version 1.000, stuff might change, but not without good reason.

Re: RFC: Concise OOP Syntax
by daxim (Curate) on Oct 21, 2019 at 12:30 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://11103898]
Approved by Tanktalus
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2021-12-06 21:45 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (33 votes). Check out past polls.