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
- $self is already shifted from @_ and directly available
Methods
- 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.
Comments?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.