develooper Front page | perl.perl5.porters | Postings from August 2009

Re: A complete design for := (bind)

Thread Previous | Thread Next
From:
David Golden
Date:
August 25, 2009 15:23
Subject:
Re: A complete design for := (bind)
Message ID:
5d4beb40908251522g32e8eabegcc0bc5bc97a94a06@mail.gmail.com
On Tue, Aug 25, 2009 at 3:48 PM, Chip Salzenberg<chip@pobox.com> wrote:
> The existing Perl 5 prototype syntax spells concepts *similar* to flatten
> and unflatten as @ and \@.  Just changing the bind syntax in that fashion,
> one ends up with:
>
>    :($a, \@b, \%c) := ...   # should have 3 elements on RHS
>    :($a,  @b,  %c) := ...   # @b ends up with N elements, %c ends up empty
>
> Good: this isn't as ugly as I expected, and kinda looks like what it does.
> Bad: It means that the backslash here:
>
>    :(\$a) := ...
>
> is either illegal or useless -- an unfortunate situation, and a warning sign
> of error, but perhaps tolerable.

I like this approach.  \$a could just warn, but see below.

> But where does one put "ro", "rw", or "copy" in this?  And what becomes the
> syntactic marker to allow parsing new keywords in the first place?  And will
> the resulting function prototypes be readable enough to use?  One wonders.

I get the sense that you're wanting to use the exact same syntax in
function signatures as in code.  What happens if you relax that
goal/constraint?  Or at least, consider those cases separately and
then look for opportunities to be consistent between them?

Among other things, it gives a potential meaning to \$a.

    sub foo(\$a, \@b) { ... } # aliasing
    sub foo($a, @b) {... } # copying

Whereas in code, copying is this:

    my ($a,$b) = @vars

Poof.  There goes the "is copy" trait. It's unnecessary.  :-)

What about readonly?  I'm not sure.  I think the important question
for signatures is whether to do it via traits or punctuation.  E.g.

    sub foo(\$a is ro, \@b is ro) { ... } # readonly alias via traits
    sub foo(:$a, :@b) { ... } # readonly alias via punctuation (with
'\' unnecessary given context of ':')

For code, I think the important question is whether it really needs to
be done all in one statement or if it could be two? (Here, just @b
would be read-only):

    bind( \$a, \@b ) := @vars;
    readonly( \@b );

Is this expect to be happening enough (outside function signatures)
that it needs to be one statement?  If one statement, then could it be
one statement that makes everything readonly?  (e.g. "bind_ro") Or one
statement that makes each variable individually ro or rw?  Or
something like this?

    readonly bind (\$a, \@b) := @vars;

For that matter, is read-only really important for this feature for
Perl 5 or is it a "nice to have"?  Naming parameters and controlling
whether they are by-value or by-reference seems far more important to
me than 'by-reference-but-readonly'.

>> I also don't terribly like :() syntax because I find it hard to skim.
>
> I'm sorry, but "that's too much punctuation" is not a credible sentiment
> from a Perl 5 programmer.  :-,

Was that an odd smiley or a newly proposed bind syntax?  Hard to tell.  :-()

>>     bind(@a) := @x;
>
> In fairness, that's pretty.

Unfortunately, I wrote it before I thought about the existing 'bind'
function.  I'd suggest 'alias' instead, unless the clash with
Data::Alias is too great.  Or some other related word or expression:
"mimic", "aka", "refer"?  (Checking against CPAN for collisions TBD.)

-- David

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About