Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

nothing

124 views
Skip to first unread message

Rod Adams

unread,
Mar 20, 2005, 10:08:08 PM3/20/05
to Perl6 Language List
Does Perl need a no-op function?

With the addition of "no bare literals", it makes constructs like

1 while some_func();

an error. I propose creating a no-op function "nothing" that can be used
here or anywhere else you specifically wish to do nothing at all.

given $this {
when Even { nothing };
when Prime { ... };
default { ... };
}


As a side question, I assume that there's a predicate form of "when",
but it's not mentioned.

given $this {
nothing when Even;

when Prime { ... };

default { ... };
}


-- Rod Adams

Larry Wall

unread,
Mar 21, 2005, 12:32:28 PM3/21/05
to Perl6 Language List
On Sun, Mar 20, 2005 at 09:08:08PM -0600, Rod Adams wrote:
: Does Perl need a no-op function?

:
: With the addition of "no bare literals", it makes constructs like
:
: 1 while some_func();
:
: an error.

Well, it's not a bareword--it's just potentially a useless use of
the value in a void context, and we could suppress that warning for
0 and 1 like we do Perl 5 without violating our "no barewords" dictum.

: I propose creating a no-op function "nothing" that can be used

: here or anywhere else you specifically wish to do nothing at all.
:
: given $this {
: when Even { nothing };
: when Prime { ... };
: default { ... };

: }

We use <null> in rules, and Ada used "null" as a keyword, so I'd probably
prefer that if there are no serious objections and if nobody has a better
idea.

: As a side question, I assume that there's a predicate form of "when",

: but it's not mentioned.
:
: given $this {
: nothing when Even;
:
: when Prime { ... };
:
: default { ... };

: }

It's mentioned somewhere, I forget where, but its semantics are
currently defined such that it would not bypass the other cases.
It's possible that's a Surprise, but we were invisioning it as a
mechanism for adding extra tests against the topic when you don't
want to break out. And we wanted to encourage people to use the
"comb structure" when they really are dealing with exclusive options.

Larry

Juerd

unread,
Mar 21, 2005, 3:59:54 PM3/21/05
to Austin Hastings, Juerd, Rod Adams, perl6-l...@perl.org
Austin Hastings skribis 2005-03-21 15:55 (-0500):
> I'd like to see nothing as just an alias for {}.
> if $expr
> {
> do nothing;
> }
> Possibly the most clear piece of P6 code ever.

Dangerous, though :)

do nothing if $input =~ /\W/;
system "rm -- $input";

But yes, an alias would be nice. There have been some cases where I
wanted to explicitly state "} else { do nothing }" to make clear that it
was not a design bug.

However, the very same thing can be done with a simple comment:

} else {
# do nothing
}

And that is exactly what I do.


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

Juerd

unread,
Mar 21, 2005, 3:42:04 PM3/21/05
to Rod Adams, perl6-l...@perl.org
Rod Adams skribis 2005-03-21 14:25 (-0600):
> if $expr {
> nothing;
> }
> is harder to get confused over, IMO

Except writing something when you mean nothing is kind of weird. It
makes sense in rules because it doesn't usually make sense to match
nothingness, but for blocks, I'd hate to see { } be invalid or meaning
anything other than the proposed nothing.

Rod Adams

unread,
Mar 21, 2005, 2:17:01 PM3/21/05
to Perl6 Language List
Larry Wall wrote:

>On Sun, Mar 20, 2005 at 09:08:08PM -0600, Rod Adams wrote:
>
>: I propose creating a no-op function "nothing" that can be used
>: here or anywhere else you specifically wish to do nothing at all.
>:
>: given $this {
>: when Even { nothing };
>: when Prime { ... };
>: default { ... };
>: }
>
>We use <null> in rules, and Ada used "null" as a keyword, so I'd probably
>prefer that if there are no serious objections and if nobody has a better
>idea.
>
>

It's potentially confusing for the database crowd, where the word "null"
is pretty much a synonym for a Perl "undef". Being part of that crowd,
"null" is invariably locked into my head as a value. It works in the
Rule sense of <null>, since you're looking for the value which is nothing.

I also think a huffman level of 7 makes more sense than 4, so I still
prefer "nothing".

That being said, I'm sure people could learn the difference if they
needed to.

>
>: As a side question, I assume that there's a predicate form of "when",
>: but it's not mentioned.
>:
>: given $this {
>: nothing when Even;
>:
>: when Prime { ... };
>:
>: default { ... };
>: }
>
>It's mentioned somewhere, I forget where, but its semantics are
>currently defined such that it would not bypass the other cases.
>It's possible that's a Surprise, but we were invisioning it as a
>mechanism for adding extra tests against the topic when you don't
>want to break out.
>

"possible" surprise? Every other conditional holds its semantics whether
in predicate or subjective form, so why should it be surprising when
"when" changes it's semantics?

If you want to test against the topic without breaking out, you still
have "if" and "$_" at your disposal, which should always be clear what
you're doing.


> And we wanted to encourage people to use the
>"comb structure" when they really are dealing with exclusive options.
>
>

What's wrong with

given somefunc() {
die "error" when 0;
print_status when 1;
prompt_options when 2;
die "impossible";
}

as an alternative to the comb in the cases where all the commands are
short? That seems cleaner to me than:

given somefunc() {
when 0 { die "error" };
when 1 { print_status };
when 2 { prompt_options };
default { die "impossible" };
}

No, the former doesn't look a lot like the classical switch statement,
but this is the land of TMTOWTDI.

-- Rod Adams


Austin Hastings

unread,
Mar 21, 2005, 3:55:48 PM3/21/05
to Juerd, Rod Adams, perl6-l...@perl.org
Juerd wrote:

>Rod Adams skribis 2005-03-21 14:25 (-0600):
>
>
>> if $expr {
>> nothing;
>> }
>>is harder to get confused over, IMO
>>
>>
>
>Except writing something when you mean nothing is kind of weird. It
>makes sense in rules because it doesn't usually make sense to match
>nothingness, but for blocks, I'd hate to see { } be invalid or meaning
>anything other than the proposed nothing.
>
>
>Juerd
>
>

I'd like to see nothing as just an alias for {}.

if $expr
{
do nothing;
}

Possibly the most clear piece of P6 code ever.

=Austin

Rod Adams

unread,
Mar 21, 2005, 10:57:07 PM3/21/05
to perl6-l...@perl.org
Juerd wrote:

>Austin Hastings skribis 2005-03-21 15:55 (-0500):
>
>
>>I'd like to see nothing as just an alias for {}.
>> if $expr
>> {
>> do nothing;
>> }
>>Possibly the most clear piece of P6 code ever.
>>
>>
>
>Dangerous, though :)
>
> do nothing if $input =~ /\W/;
> system "rm -- $input";
>
>But yes, an alias would be nice. There have been some cases where I
>wanted to explicitly state "} else { do nothing }" to make clear that it
>was not a design bug.
>
>However, the very same thing can be done with a simple comment:
>
> } else {
> # do nothing
> }
>
>And that is exactly what I do.
>
>

But you can't solve

nothing while something();

with a comment. Well, not gracefully. Not without inline comments, at least.


There seems to be some support for just saying:

{} while something();


I would also hope the compiler could optimize away something like:

sub nothing () {};

-- Rod Adams

0 new messages