develooper Front page | perl.perl6.language | Postings from March 2009

Re: r25807 - docs/Perl6/Spec

Thread Previous
Jon Lang
March 14, 2009 14:30
Re: r25807 - docs/Perl6/Spec
Message ID:
On Sat, Mar 14, 2009 at 7:29 AM, Larry Wall <> wrote:
> : So if I were to say:
> :
> :     rand $n:
> :
> : is the compiler smart enough to notice that trailing colon and
> : recognize this as an indirect method call rather than two adjacent
> : terms?
> No, currently under STD you get:
>    Obsolete use of rand(N); in Perl 6 please use N.rand or (1..N).pick instead at (eval) line 1:
> : Or would I have to say:
> :
> :     rand($n:)
> :
> : to get the indirect method call?
> That would work, but then why not:
>    rand*$n
>    $n*rand
>    $n.rand
>    (1..$n).pick
> In fact, given that you usually want to integerize anyway, I could
> almost argue myself out of supporting the $n.rand form as well...

It's largely a matter of principle: if I can say $, I expect to
be able to say foo $x: as well.  Every time you introduce an exception
to the rule, you're throwing something in that has the potential to
cause confusion; so you should do so with some caution.  I think that
"best uses" should include something to the effect of "avoid using the
same identifier for both a term and a routine".  IMHO, rand should
either be a term or a method; but not both.

There are also some linguistic reasons for this "best uses" proposal:
people tend to think of terms as nouns and routines as verbs.  And
gerunds are more akin to "&foo" than to "term:<foo>".

Left-field idea here: there was recently some discussion on this list
about the possibility of continuous ranges, which would be in contrast
to how 1..$n is a discrete list of options.  If you were to do this,
then you could use .pick on a continuous range to generate a random
number anywhere within its bounds.  So:

    (1 to 5).pick

(where infix:<to> creates a continuous range, inclusive of both
boundaries) would in theory be as likely to return 2.5 or pi as 3.
IMHO, this does a better job of handling what most people want rand to
do when they start thinking in terms of assigning parameters to it.
And with that in place, rand could become a term that's short for
something like:

    pick (0 to^ 1):

Jonathan "Dataweaver" Lang

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About