proposal: binding with a function

2 views
Skip to first unread message

BÁRTHÁZI András

unread,
Jun 14, 2005, 8:06:37 AM6/14/05
to perl6-l...@perl.org
Hi,

As I know, for binding, you can use the := operator, and just this:

$a := $b;

I would like to make a proposal, based on Ruby[1]:

alias $a, $b;

It's a fun:

sub newline {
"\n" x $_;
}

alias newlines, newline;

So you can write:

print 5.enters;

Currently, you have to write it a more "uglier" way:

my &newlines := &newline;

Anyway, char '&' is really neccesary there? It should work w/o it, too, as
I think. Now - in Pugs - it doesn't.

Bye,
Andras

[1] http://www.zenspider.com/Languages/Ruby/QuickRef.html#29

Larry Wall

unread,
Jun 14, 2005, 12:44:47 PM6/14/05
to perl6-l...@perl.org
On Tue, Jun 14, 2005 at 02:06:37PM +0200, BÁRTHÁZI András wrote:
: Hi,

:
: As I know, for binding, you can use the := operator, and just this:
:
: $a := $b;
:
: I would like to make a proposal, based on Ruby[1]:
:
: alias $a, $b;

You can always write a macro that does that.

: It's a fun:


:
: sub newline {
: "\n" x $_;
: }
:
: alias newlines, newline;

At one point we had a macro variant for single words:

word enters {'newline'};

But it's not clear which syntactic categories pay attention to that.
It's obvious that your intent is to alias a method name, here, but
that's not at all obvious to the compiler. Most simple macros remap
terms, not method names, which are a subset of postfix operators that
require the dot.

: So you can write:
:
: print 5.enters;
:
: Currently, you have to write it a more "uglier" way:
:
: my &newlines := &newline;

That won't work on a method name anyway unless you do it in the
dispatch class.

: Anyway, char '&' is really neccesary there? It should work w/o it, too, as

: I think. Now - in Pugs - it doesn't.

The point of the sigils is to help keep the namespaces and syntactic
categories straight, and to indicate you're talking about the actual
variable object rather than just a name. Since you're merely trying
to remap the name here and not the object (which isn't even a variable
in your own package), the use of & is going to be inappropriate in
any event. What you're really looking for is some kind of macro
based syntactic sugar that maps the appropriate syntactic category.
Maybe something based on this:

my macro postfix:<enters> { 'newline' }

There's some possibilility of syntactic sugar resembling:

my postfix:<enters> ::= <newline>;

But Perl 6 is not going to encourage the C-preprocessor mindset of
completely divorcing the meaning of identifiers from their syntactic
context. In other words, you can't wildcard the syntactic category:

*:<enters> ::= <newline>;

You'll have to write your own macro if you want to do that.

Larry

BÁRTHÁZI András

unread,
Jun 15, 2005, 2:36:49 AM6/15/05
to Larry Wall, perl6-l...@perl.org
Larry,

> You can always write a macro that does that.

> [...]

> That won't work on a method name anyway unless you do it in the
> dispatch class.

> [...]


> You'll have to write your own macro if you want to do that.

As I understood, you wrote down, how I can workaround it with macros,
and why it wouldn't work well. As I see, there's no a nice solution to
alias a method or a sub, even with macros.

But you didn't wrote, why are you against the 'alias'?

Bye,
Andras

Carl Franks

unread,
Jun 15, 2005, 3:40:12 AM6/15/05
to perl6-l...@perl.org
> : alias newlines, newline;

Isn't it possible to add a Role to the relevant Class, which specifies
that is 'handles' the method name you want as an alias?

Carl

BÁRTHÁZI András

unread,
Jun 15, 2005, 4:54:11 AM6/15/05
to Carl Franks, perl6-l...@perl.org
Hi,

If it's possible, it would be fine for me in this particular case. Is it
possible?

Anyway, IMHO this alias function can be useful in other cases, too.

Bye,
Andras

BÁRTHÁZI András

unread,
Jun 20, 2005, 11:03:31 AM6/20/05
to BÁRTHÁZI András, Larry Wall, perl6-l...@perl.org
Hi,

I'm still interested in, why "alias" wouldn't be a native Perl 6 term?

I think, there are several reasons for "alias":

- in natural languages, synonims are very often - alias is a synonim
- in Perl 6, currently there's no way to create a reference to a
variable, _with the context of the variable_, too (binding just give
me possibility to bind a variable into another, but the new variable
won't be automatically have the same context, as the binded one)

Some new examples, maybe better than before:

sub kilobytes ($value:) is export {
return $value*1024;
}
alias kilobytes, kilobyte;

So both routines will be an exported one. You can use this code then:

say 1.kilobyte;
say 2.kilobytes;

Bye,
Andras

Abhijit Mahabal

unread,
Jun 20, 2005, 11:39:18 AM6/20/05
to BÁRTHÁZI András, Larry Wall, perl6-l...@perl.org
On Mon, 20 Jun 2005, BÁRTHÁZI András wrote:

> Hi,
>
> I'm still interested in, why "alias" wouldn't be a native Perl 6 term?
>
> I think, there are several reasons for "alias":

I am not arguing against alias, but just wanted to point out something.

> - in Perl 6, currently there's no way to create a reference to a
> variable, _with the context of the variable_, too

unless you bind immediately where you declare the original variable:

> Some new examples, maybe better than before:
>
> sub kilobytes ($value:) is export {
> return $value*1024;
> }
> alias kilobytes, kilobyte;

replace the last line with:
&kilobytes := &kilobyte;

and the scoping is not an issue.

And with synonyms, binding as soon as declaring seems prudent.

--abhijit

Abhijit Mahabal http://www.cs.indiana.edu/~amahabal/

Brent 'Dax' Royal-Gordon

unread,
Jun 20, 2005, 1:43:19 PM6/20/05
to BÁRTHÁZI András, Larry Wall, perl6-l...@perl.org
On 6/20/05, BÁRTHÁZI András <and...@barthazi.hu> wrote:
> - in natural languages, synonims are very often - alias is a synonim

Perl is modeled on natural languages, but that doesn't mean it is one.
At its core, Perl is a limited, artificial language being explicitly
designed with certain goals. One of those goals is that it should be
as small as possible given the feature set we want it to support; an
`alias` built-in that essentially duplicates an existing feature goes
against that goal.

> - in Perl 6, currently there's no way to create a reference to a
> variable, _with the context of the variable_, too (binding just give
> me possibility to bind a variable into another, but the new variable
> won't be automatically have the same context, as the binded one)

I'm not sure what you mean by "context" here. Context has a very
specific meaning in Perl, representing the type a function's caller is
expecting; this doesn't seem to be what you're talking about here.

> alias kilobytes, kilobyte;

This is a couple punctuation symbols short of:
&kilobytes := &kilobyte;
Or maybe:
&kilobytes ::= &kilobyte;
I'm not really sure what behavior you have in mind for alias.

(By the way, a simple name like "alias" is ambiguous about argument
order, where an operator isn't.)

--
Brent 'Dax' Royal-Gordon <br...@brentdax.com>
Perl and Parrot hacker

BÁRTHÁZI András

unread,
Jun 20, 2005, 1:55:48 PM6/20/05
to br...@brentdax.com, Larry Wall, perl6-l...@perl.org
Hi,

>>- in natural languages, synonims are very often - alias is a synonim
>
> Perl is modeled on natural languages, but that doesn't mean it is one.
> At its core, Perl is a limited, artificial language being explicitly
> designed with certain goals. One of those goals is that it should be
> as small as possible given the feature set we want it to support; an
> `alias` built-in that essentially duplicates an existing feature goes
> against that goal.

I can agree with it, but I think it would be a great feature. And it
doesn't depends on Perl 6, but it depends on Parrot, as I think.

>>- in Perl 6, currently there's no way to create a reference to a
>> variable, _with the context of the variable_, too (binding just give
>> me possibility to bind a variable into another, but the new variable
>> won't be automatically have the same context, as the binded one)
>
> I'm not sure what you mean by "context" here. Context has a very
> specific meaning in Perl, representing the type a function's caller is
> expecting; this doesn't seem to be what you're talking about here.
>
>>alias kilobytes, kilobyte;
>
> This is a couple punctuation symbols short of:
> &kilobytes := &kilobyte;
> Or maybe:
> &kilobytes ::= &kilobyte;
> I'm not really sure what behavior you have in mind for alias.

"&kilobytes := &kilobyte;" will not work for you (try it), because you
have to declare the "variable" kilobytes - in the default strict mode.
But you can't say for ex. "my &kilobytes", if you want to export it. I
would like to copy if the subroutine/variable is local, or exported,
or... etc. Oh, I have the term: I would like to copy the _scope_ of it,
too. Forget the "context". Simple binding shouldn't have to copy the
scope, too.

Anyway, "alias" is a Ruby term, and if Parrot will be able to support
Ruby, then it will be able to support this function, too.

Bye,
Andras

Brent 'Dax' Royal-Gordon

unread,
Jun 20, 2005, 2:06:10 PM6/20/05
to BÁRTHÁZI András, Larry Wall, perl6-l...@perl.org
On 6/20/05, BÁRTHÁZI András <and...@barthazi.hu> wrote:

So you say `our &kilobytes ::= &kilobyte` (or `:=`, you still haven't
said if alias works at compile time or runtime) and call it a day.

IIUC, traits like `is exported` are attached to the container, not the
name; since aliasing connects a name to a container, you should be
fine on that front. (If it doesn't work, that's because `is exported`
does something funky that `alias` would have to treat as a special
case; certainly other traits like `is rw` would follow a
`:=`-binding.)

> Anyway, "alias" is a Ruby term, and if Parrot will be able to support
> Ruby, then it will be able to support this function, too.

As I've said before, Perl supports `alias`--it's just spelled `:=`.

BÁRTHÁZI András

unread,
Jun 20, 2005, 2:25:45 PM6/20/05
to br...@brentdax.com, Larry Wall, perl6-l...@perl.org
Hi,

>>"&kilobytes := &kilobyte;" will not work for you (try it), because you
>>have to declare the "variable" kilobytes - in the default strict mode.
>>But you can't say for ex. "my &kilobytes", if you want to export it.
>
> So you say `our &kilobytes ::= &kilobyte` (or `:=`, you still haven't
> said if alias works at compile time or runtime) and call it a day.
>
> IIUC, traits like `is exported` are attached to the container, not the
> name; since aliasing connects a name to a container, you should be
> fine on that front. (If it doesn't work, that's because `is exported`
> does something funky that `alias` would have to treat as a special
> case; certainly other traits like `is rw` would follow a
> `:=`-binding.)
>
>>Anyway, "alias" is a Ruby term, and if Parrot will be able to support
>>Ruby, then it will be able to support this function, too.
>
> As I've said before, Perl supports `alias`--it's just spelled `:=`.

If you're right, then I'm happy. I don't want "alias", I would like to
get it's behaviour. In Ruby, I think it's a compile time feature, but
don't know, I'm not programming in Ruby.

I'm not (yet) an expert in Perl 6, so sorry if I'm not right, but "is
exported" is about the name, not the container, as I think. If you
attach an "is exported" to a class method, you can reach it from
everywhere, just using it's name. If you say _in your class_ that "our
&mysub ::= &exportedsub", then you can reach the mysub sub just in the
class, not from everywhere in your program (as a plain sub). I'm really
not sure, if I'm right, so please tell me, that I lost, and I will be
happy. :)

Bye,
Andras

Piers Cawley

unread,
Jun 23, 2005, 10:30:32 AM6/23/05
to br...@brentdax.com, BÁRTHÁZI András, Larry Wall, perl6-l...@perl.org
Brent 'Dax' Royal-Gordon <bren...@gmail.com> writes:
> As I've said before, Perl supports `alias`--it's just spelled `:=`.

Here's a rubyish idiom:

my &old_behaviour := &function;

&function := sub { try_some_stuff || &old_behaviour }

Except, with binding it doesn't work like that, you end up with an infinite
loop.

Juerd

unread,
Jun 23, 2005, 11:02:59 AM6/23/05
to Piers Cawley, br...@brentdax.com, BÁRTHÁZI András, Larry Wall, perl6-l...@perl.org
Piers Cawley skribis 2005-06-23 15:30 (+0100):

I still think subs should have a value, than can be copied :)

my &old_behaviour = &function;
&function = sub { try_something || &old_behaviour };


Juerd
--
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html
http://convolution.nl/gajigu_juerd_n.html

Ingo Blechschmidt

unread,
Jun 23, 2005, 11:20:14 AM6/23/05
to perl6-l...@perl.org
Hi,

Juerd <juerd <at> convolution.nl> writes:
> Piers Cawley skribis 2005-06-23 15:30 (+0100):
> > Brent 'Dax' Royal-Gordon <brentdax <at> gmail.com> writes:
> > > As I've said before, Perl supports `alias`--it's just spelled `:=`.
> > Here's a rubyish idiom:
> > my &old_behaviour := &function;
> > &function := sub { try_some_stuff || &old_behaviour }
> > Except, with binding it doesn't work like that, you end up with an
infinite
> > loop.
>
> I still think subs should have a value, than can be copied :)
>
> my &old_behaviour = &function;
> &function = sub { try_something || &old_behaviour };

I absolutely agree!

But: Your example won't work correctly, as function() would
return a Code reference (&old_behaviour) instead of calling
old_behaviour, wouldn't it?

I think all of the following should work:

my &old_behaviour = &function;
&function = sub { try_something() || old_behaviour() };

my &old_behaviour = &function;
&function := sub { try_something() || old_behaviour() };

Additionally, with nothingmuch's lazy {...} proposal, the
following should work, too (I think):

my &old_behaviour = &function;
&function = lazy { &try_something || &old_behaviour };

my &old_behaviour = &function;
&function := lazy { &try_something || &old_behaviour };


--Ingo

--
Linux, the choice of a GNU | When cryptography is outlawed, bayl bhgynjf
generation on a dual AMD | jvyy unir cevinpl!
Athlon! |

Damian Conway

unread,
Jun 23, 2005, 6:47:53 PM6/23/05
to perl6-l...@perl.org
Piers Cawley wrote:

> Here's a rubyish idiom:
>
> my &old_behaviour := &function;
>
> &function := sub { try_some_stuff || &old_behaviour }
>
> Except, with binding it doesn't work like that, you end up with an infinite
> loop.

But this version *should* work correctly:

# Bind the name '&old_behaviour' to the closure that is currently bound to
# the name '&function'...
my &old_behaviour := &function;

# Rebind the name '&function' to the specified closure, calling the
# old behaviour (via its new name) if necessary...
&function := sub { try_some_stuff || old_behaviour(*@_) };

No infinite loop involved.

Perl 6 even has specific syntactic sugar for this kind of thing:

&function.wrap( { try_some_stuff || call(*@_) } );

See: http://dev.perl.org/perl6/doc/design/syn/S06.html#Wrapping

Damian

Reply all
Reply to author
Forward
0 new messages