Discussion:
[Pdl-porters] PDL3 method call syntax idea
Chris Marshall
2013-12-08 15:52:52 UTC
Permalink
One perl5 wart that seems to bother folks trying out
PDL is the fact that our method call syntax is via ->
while C++ and Python and others ended up using
a dot (I think perl6 uses . as well).

I wondered if we could add that as a possible
modification for PDL3. I figured we could just use
PDL::NiceSlice3 to hack it in---admittedly not to
be preferred. Then it occurred to me, what about
simply overloading the . operator. I don't know if
the precedence would work out but it might be
doable.

Wild or workable...comments?
Chris
Chris Marshall
2013-12-08 15:56:24 UTC
Permalink
If this ends up working, maybe the perl5 MOP might
add it to the mix. Then any modern OO perl module
could use . instead of -> if desired. That could improve
the perl5 image as a modern language for drive by
shoppers at the Language Mart. :-)


On Sun, Dec 8, 2013 at 10:52 AM, Chris Marshall <***@gmail.com> wrote:
> One perl5 wart that seems to bother folks trying out
> PDL is the fact that our method call syntax is via ->
> while C++ and Python and others ended up using
> a dot (I think perl6 uses . as well).
>
> I wondered if we could add that as a possible
> modification for PDL3. I figured we could just use
> PDL::NiceSlice3 to hack it in---admittedly not to
> be preferred. Then it occurred to me, what about
> simply overloading the . operator. I don't know if
> the precedence would work out but it might be
> doable.
>
> Wild or workable...comments?
> Chris
David Mertens
2013-12-09 14:14:05 UTC
Permalink
Cool idea, but... wouldn't this require special bareword handling for the
left side of the dot operator?

$output = $object . foo(something);

Hmm...

I believe that the "method" call would take place before the object binding
(i.e. concatenation). How do we get this to work? We create functions which
return a curried method. That is, foo and its curry could look like this:

sub foo {
my ($self, $arg1, $arg2, ...) = @_
# real work goes on here
}

sub foo_binder {
my @args = @_;
return sub {
my $self = shift;
$self->foo(@_, @args);
}
}

# Export it properly
*Calling::Package{foo} = \&foo_binder;

Notice that I export foo_binder() into the calling package under the name
foo(). This is critical. Furthermore, given the way that @_ is modified
before the goto, this still allows users to call the method as a function
with the piddle as the first argument. Now, we just need to write a binding
function:

# Overload the dot operator, which we'll call bind, to this:
sub bind {
my ($self, $curry) = @_;
$curry->($self);
}

Thus, $piddle.method($arg1) would do what we want.

The interesting thing about this is that we get the chain of method calls
*before* invoking them on the piddle. It smells to me like we might be able
to perform some optimizations under the hood with this sort of scheme.
Obviously we would have to return something a bit more complex than a
simple curry---it would have to support some sort of query
capabilities---but the possibilities are there.

Very cool, Chris!

David


On Sun, Dec 8, 2013 at 10:56 AM, Chris Marshall <***@gmail.com>wrote:

> If this ends up working, maybe the perl5 MOP might
> add it to the mix. Then any modern OO perl module
> could use . instead of -> if desired. That could improve
> the perl5 image as a modern language for drive by
> shoppers at the Language Mart. :-)
>
>
> On Sun, Dec 8, 2013 at 10:52 AM, Chris Marshall <***@gmail.com>
> wrote:
> > One perl5 wart that seems to bother folks trying out
> > PDL is the fact that our method call syntax is via ->
> > while C++ and Python and others ended up using
> > a dot (I think perl6 uses . as well).
> >
> > I wondered if we could add that as a possible
> > modification for PDL3. I figured we could just use
> > PDL::NiceSlice3 to hack it in---admittedly not to
> > be preferred. Then it occurred to me, what about
> > simply overloading the . operator. I don't know if
> > the precedence would work out but it might be
> > doable.
> >
> > Wild or workable...comments?
> > Chris
>
> _______________________________________________
> PDL-porters mailing list
> PDL-***@jach.hawaii.edu
> http://mailman.jach.hawaii.edu/mailman/listinfo/pdl-porters
>



--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." -- Brian Kernighan
David Mertens
2013-12-09 14:17:14 UTC
Permalink
Oops, sorry, I was figuring all this out as I wrote, and mis-spoke. This
bit:

... given the way that @_ is modified before the goto, this still allows
> users to call the method as a function with the piddle as the first
> argument.
>

That should read thus:

... the method is invoked in such a way that users can call the method as a
> function with the piddle as the first argument.
>

I had originally considered a goto, but that would not preserve
inheritance. Sorry about that.

David
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." -- Brian Kernighan
Chris Marshall
2013-12-09 15:09:37 UTC
Permalink
On Mon, Dec 9, 2013 at 9:14 AM, David Mertens <***@gmail.com> wrote:
> Cool idea, but... wouldn't this require special bareword handling for the
> left side of the dot operator?

Thanks for the implementation idea, David.

> ...snip...
>
> The interesting thing about this is that we get the chain of method calls
> *before* invoking them on the piddle. It smells to me like we might be able
> to perform some optimizations under the hood with this sort of scheme.
> Obviously we would have to return something a bit more complex than a simple
> curry---it would have to support some sort of query capabilities---but the
> possibilities are there.

This ability is also present with the proposed move from PDL
as a hash (optionally) to a Moo[se] based object. However,
the details and timing of the calls would vary.

> Very cool, Chris!

Perl purists would disagree. My idea was to address of the
dark universe of PDL users and what it would take to build
a more active and larger user and developer community. :-)

> David
>
>
> On Sun, Dec 8, 2013 at 10:56 AM, Chris Marshall <***@gmail.com>
> wrote:
>>
>> If this ends up working, maybe the perl5 MOP might
>> add it to the mix. Then any modern OO perl module
>> could use . instead of -> if desired. That could improve
>> the perl5 image as a modern language for drive by
>> shoppers at the Language Mart. :-)
>>
>>
>> On Sun, Dec 8, 2013 at 10:52 AM, Chris Marshall <***@gmail.com>
>> wrote:
>> > One perl5 wart that seems to bother folks trying out
>> > PDL is the fact that our method call syntax is via ->
>> > while C++ and Python and others ended up using
>> > a dot (I think perl6 uses . as well).
>> >
>> > I wondered if we could add that as a possible
>> > modification for PDL3. I figured we could just use
>> > PDL::NiceSlice3 to hack it in---admittedly not to
>> > be preferred. Then it occurred to me, what about
>> > simply overloading the . operator. I don't know if
>> > the precedence would work out but it might be
>> > doable.
>> >
>> > Wild or workable...comments?
>> > Chris
Chris Marshall
2013-12-09 15:59:47 UTC
Permalink
On Sun, Dec 8, 2013 at 10:52 AM, Chris Marshall <***@gmail.com> wrote:
>
> One perl5 wart that seems to bother folks trying out
> PDL is the fact that our method call syntax is via ->
> while C++ and Python and others ended up using
> a dot (I think perl6 uses . as well).

I forgot the elephant in the room: MATLAB/Octave use a dot
for their OO deref as well. Even if we don't allow this feature
generally, a PDL::Matlab module that would make PDL
more Matlab/Octave user friendly would be nice to have....

> I wondered if we could add that as a possible
> modification for PDL3. I figured we could just use
> PDL::NiceSlice3 to hack it in---admittedly not to
> be preferred. Then it occurred to me, what about
> simply overloading the . operator. I don't know if
> the precedence would work out but it might be
> doable.
>
> Wild or workable...comments?
> Chris
Judd Taylor
2014-01-10 15:40:22 UTC
Permalink
My opinion on this is that I don't like it.

I think one of the major negatives I always hear about Perl and PDL is that the syntax is so varied. It's often hard to read other's code, and everybody is always doing something different.

Supporting the '.' IMO, is just more of the same, making things more confusing for users in the end.

I mean, we already have 'or' and '||', maybe we can have 'dot' and '.' and 'arrow' and '->'. That would be very Perl-like ;)

This is not confusing at all, for example:
$result = $pdl dot slice(";") . $pdl2 arrow index(3);


So, that's my curmudgeon opinion. In a related note, it would be a good idea IMO, to try to unify the syntax that everybody is using all over PDL, which has even more variance that most perl. We have multiple ways to slice (nice slice, which IMO I will never use as it's confusing to read when you are used to using symbolic references), and the documentation, demos, and examples are not all using the same PDL style.

If it were my project, I would have someone go over everything to ensure a consistent style is used everywhere in the documentation, and then have a separate doc for each style variant where just that variant is discussed on its own.

I think something like this will go a long way towards attracting users. There's nothing more frustrating that going by old docs, asking a question which is then answered with code you can't even recognize or read, and then scratching your head and not learning anything in the process because the code you have that now works doesn't match the docs you're using. It's like we've created a tower of babel here.

Again, this is just my opinion on the subject... feel free to disregard if you want.
____________________________
Judd Taylor
Software Engineer

Orbital Systems, Ltd.
3807 Carbon Rd.
Irving, TX 75038-3415

(972) 915-3669 x127

________________________________________
From: Chris Marshall [***@gmail.com]
Sent: Monday, December 09, 2013 9:59 AM
To: pdl-porters
Subject: Re: [Pdl-porters] PDL3 method call syntax idea

On Sun, Dec 8, 2013 at 10:52 AM, Chris Marshall <***@gmail.com> wrote:
>
> One perl5 wart that seems to bother folks trying out
> PDL is the fact that our method call syntax is via ->
> while C++ and Python and others ended up using
> a dot (I think perl6 uses . as well).

I forgot the elephant in the room: MATLAB/Octave use a dot
for their OO deref as well. Even if we don't allow this feature
generally, a PDL::Matlab module that would make PDL
more Matlab/Octave user friendly would be nice to have....

> I wondered if we could add that as a possible
> modification for PDL3. I figured we could just use
> PDL::NiceSlice3 to hack it in---admittedly not to
> be preferred. Then it occurred to me, what about
> simply overloading the . operator. I don't know if
> the precedence would work out but it might be
> doable.
>
> Wild or workable...comments?
> Chris
David Mertens
2014-01-10 15:58:57 UTC
Permalink
Judd, all -

I just re-read through this thread. When I said, "Cool idea!" what I meant
was that it was cool that Perl could actually support such a construction.
Whether it is a good idea or not is a different question. In light of
Judd's observations, I suppose this would be one more way that PDL would
make itself distinct from normal Perl, and I'm not really a fan of that.
Niceslice aside, PDL is a library, not a language extension. The way that
the docs talk about being able to write "pdl" scripts, and even being able
to put "#!/usr/local/pdl" in your shebang line (or at least, I gathered
this was possible once upon a time) makes it easy for a novice to get the
misconception that pdl is actually a distinct language from Perl.

As for documentation style consistencies, I think that a cleanup would be
wonderful. I don't even care how that cleanup would look, just so long as
the documentation style was specified somewhere so we could easily point
new contributors to it. I do not have the time at the moment to
substantially contribute to a docs cleanup.

David


On Fri, Jan 10, 2014 at 10:40 AM, Judd Taylor <***@orbitalsystems.com>wrote:

> My opinion on this is that I don't like it.
>
> I think one of the major negatives I always hear about Perl and PDL is
> that the syntax is so varied. It's often hard to read other's code, and
> everybody is always doing something different.
>
> Supporting the '.' IMO, is just more of the same, making things more
> confusing for users in the end.
>
> I mean, we already have 'or' and '||', maybe we can have 'dot' and '.' and
> 'arrow' and '->'. That would be very Perl-like ;)
>
> This is not confusing at all, for example:
> $result = $pdl dot slice(";") . $pdl2 arrow index(3);
>
>
> So, that's my curmudgeon opinion. In a related note, it would be a good
> idea IMO, to try to unify the syntax that everybody is using all over PDL,
> which has even more variance that most perl. We have multiple ways to slice
> (nice slice, which IMO I will never use as it's confusing to read when you
> are used to using symbolic references), and the documentation, demos, and
> examples are not all using the same PDL style.
>
> If it were my project, I would have someone go over everything to ensure a
> consistent style is used everywhere in the documentation, and then have a
> separate doc for each style variant where just that variant is discussed on
> its own.
>
> I think something like this will go a long way towards attracting users.
> There's nothing more frustrating that going by old docs, asking a question
> which is then answered with code you can't even recognize or read, and then
> scratching your head and not learning anything in the process because the
> code you have that now works doesn't match the docs you're using. It's like
> we've created a tower of babel here.
>
> Again, this is just my opinion on the subject... feel free to disregard if
> you want.
> ____________________________
> Judd Taylor
> Software Engineer
>
> Orbital Systems, Ltd.
> 3807 Carbon Rd.
> Irving, TX 75038-3415
>
> (972) 915-3669 x127
>
> ________________________________________
> From: Chris Marshall [***@gmail.com]
> Sent: Monday, December 09, 2013 9:59 AM
> To: pdl-porters
> Subject: Re: [Pdl-porters] PDL3 method call syntax idea
>
> On Sun, Dec 8, 2013 at 10:52 AM, Chris Marshall <***@gmail.com>
> wrote:
> >
> > One perl5 wart that seems to bother folks trying out
> > PDL is the fact that our method call syntax is via ->
> > while C++ and Python and others ended up using
> > a dot (I think perl6 uses . as well).
>
> I forgot the elephant in the room: MATLAB/Octave use a dot
> for their OO deref as well. Even if we don't allow this feature
> generally, a PDL::Matlab module that would make PDL
> more Matlab/Octave user friendly would be nice to have....
>
> > I wondered if we could add that as a possible
> > modification for PDL3. I figured we could just use
> > PDL::NiceSlice3 to hack it in---admittedly not to
> > be preferred. Then it occurred to me, what about
> > simply overloading the . operator. I don't know if
> > the precedence would work out but it might be
> > doable.
> >
> > Wild or workable...comments?
> > Chris
>
> _______________________________________________
> PDL-porters mailing list
> PDL-***@jach.hawaii.edu
> http://mailman.jach.hawaii.edu/mailman/listinfo/pdl-porters
>
> _______________________________________________
> PDL-porters mailing list
> PDL-***@jach.hawaii.edu
> http://mailman.jach.hawaii.edu/mailman/listinfo/pdl-porters
>



--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." -- Brian Kernighan
Loading...