Saturday, December 10, 2011

A kind of call by name

I often write code like this:
$self->create_user( username => $username, email => $email, pass_token => $pass_token );
I wish I could get rid of the naming redundancy in this call:
$self->create_user( $username, $email, $pass_token );
(without changing 'create_user' of course).

Probably some new syntax would be needed.

4 comments:

Dawid Nowak said...

I guess that the future are ”self-recognizing” parameters: right now you have to pass either an associative array of parameters, where every var is recognized by name or pass all the params in a proper order, so that the function could recognize the params by that.

What could be great? If you could pass ANY parameter and the method would "know" what to do next.

I mean, in the end, this could work as just passing a kind of meta-class object that would be easily recognized by a method and handled.

Right now ”the meta” object is a programmer who knows what to do with what stuff, hopefully there is gonna be a software that knows what to do with itself (someday :)).

I feel that I've gone to much into future :]]]

ttjjss said...

A new syntax? Perl 6 to the rescue :)

foo(:$bar, :$asd) is an equivalent of
foo(bar => $bar, asd => $asd). Possibly there's something on CPAN copying that behaviour, and if not, it should be possible with Devel::Declare.

zby said...

Yeah 'foo(:$bar, :$asd)' would be perfect :)

Nicolas Mendoza said...

how about: http://my.opera.com/nicomen/blog/using-named-parameters-without-names-when-calling-functions-in-perl