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

Statement modifiers

6 views
Skip to first unread message

Matthijs Van Duin

unread,
Mar 10, 2003, 4:52:21 AM3/10/03
to perl6-l...@perl.org
Hi all, just dropping in with some thoughts I had while reading the
archive of this list. I've tried to search the list but it's difficult
with perl keywords being common english words and google unable to search
for punctuation; if the stuff below has already been fully resolved, I'd
appreciate some pointers to the corresponding messages. :-)

Anyway, let me start by adding to the statistics: I very much like
method <~ $obj and $arg ~> sub and I like support of Unicode aliases
for operators as long as plain ascii versions remain available too.

Now the real subject.. has the issue of multiple statement modifiers
already been settled? I saw some mention it wasn't going to be supported,
but also mentions of how it would be useful; I can think of such a
situation myself:

.method when MyClass given $obj;
as alternative to:
$obj.method if $obj.isa(MyClass);

except without duplicating $obj, which may be worthwhile if it's a longer
expression. If multiple modifiers are really a no-no, then I think at
least the conditionals (if, unless, when) could be made lowest-precedence
right-associative infix operators, and leave the status of "statement
modifier" for loops and topicalizers.

This would mean that the above would be valid, and also things like:
.. if .. if .. for ..; But that multiple nested loops would be illegal
using modifiers and would require a real block. (which makes some sense,
since it's hard to think of a construction where multiple loop-modifiers
would be useful: if you have ... for @a for @b then you'd be unable to
use the @b-element since $_ would be the loop var of the inner loop)

I also think this gives a nice symmetry of various operators that only
differ in L2R/R2L and precedence (plus the ability to overload ofcourse):

$x and $y $y if $x
$x or $y $y unless $x
$x . $y $y <~ $x
$x ( $y ) $y ~> $x

Which I personally think is a Good Thing: I like to structure my code to
put the most important part of a statement on the left and exceptional
cases and details on the right. Having multiple operators with different
precedence (&&, and, if) also helps avoiding lots of parentheses, which I
think is another Good Thing because they make code look cluttered. When
I want visual grouping I prefer to use extra whitespace. Perhaps it's
not as maintainable, but it is more readable imho.

Hmmm.. and I just realized.. is something like 'print while <>;' still
available in perl 6? And if so, that means the while-loop would
topicalize in this case? What would the criterium be for "this case"? (I
hope not the kludge it is right now in perl 5 ;-)

--
Matthijs van Duin -- May the Forth be with you!

Paul

unread,
Mar 10, 2003, 11:20:39 AM3/10/03
to Matthijs van Duin, perl6-l...@perl.org

--- Matthijs van Duin <p...@nubz.org> wrote:
> Now the real subject.. has the issue of multiple statement modifiers
> already been settled? I saw some mention it wasn't going to be
> supported, but also mentions of how it would be useful; I can think
> of such a situation myself:
>
> .method when MyClass given $obj;
> as alternative to:
> $obj.method if $obj.isa(MyClass);

I think this is an unusually clear case, and even then has problems.
The real nightmare tends to show up when you duplicate a modifier.
What does

.method given $x given $y; # which object's .method is called?

mean? It gets worse below....

> except without duplicating $obj, which may be worthwhile if it's a
> longer expression. If multiple modifiers are really a no-no, then
> I think at least the conditionals (if, unless, when) could be made
> lowest-precedence right-associative infix operators, and leave the
> status of "statement modifier" for loops and topicalizers.

lowest? why lowest? Careful with that.... If you make it a lowest
precedence operator,

$x = $y if $z; # = is higher precedence

Does it get assigned if $z is undefined?



> This would mean that the above would be valid, and also things like:
> .. if .. if .. for ..;

I may be missing something, but

print if $x if $y; # ??

Are you saying "test $y, and if it's true, test $x, and if it's true
then print"? I suppose that might work....but that still makes it high
priority, doesn't it?

> But that multiple nested loops would be illegal using modifiers and
> would require a real block. (which makes some sense, since it's hard
> to think of a construction where multiple loop-modifiers would be
> useful: if you have ... for @a for @b then you'd be unable to
> use the @b-element since $_ would be the loop var of the inner loop)

Maybe not in p6.

print "$x,$y\n" for $x -> @x for $y -> @y; # is that approximate?

Ok, this is hurting my head, and I think I might hurt someone who left
me to maintain it, but I could see how it could be useful, and I think
I see how it could be parsed.... It would be like

for $y -> @y {
for $x -> @x {
print "$x,$y\n";
}
}

My question is that, though TMTOWTDI is a Good Thing, and in general
dictating style is a Bad Thing, is this much flexibility a Good Thing
or a Bad Thing? And more importantly, will the people writing the
parser become homicidal if it is decided this should be implemented?

Still,

print for @x for @y; # @y's topic masked

would probably make no sense unless it's a rather twisted form of
recursion, and for that I'd recommend writing a function rather than
setting up reference loops....

> I also think this gives a nice symmetry of various operators that
> only differ in L2R/R2L and precedence (plus the ability to overload
> ofcourse):
>
> $x and $y $y if $x
> $x or $y $y unless $x
> $x . $y $y <~ $x
> $x ( $y ) $y ~> $x

I have no idea what you mean by this.

Paul

__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/

Matthijs Van Duin

unread,
Mar 10, 2003, 12:31:08 PM3/10/03
to perl6-l...@perl.org, Hod...@writeme.com
On Mon, Mar 10, 2003 at 08:20:39AM -0800, Paul wrote:
>The real nightmare tends to show up when you duplicate a modifier.
>What does
>
> .method given $x given $y; # which object's .method is called?
>
>mean? It gets worse below....

I made a mistake in my original post, they definitely need to be left-
associative. Your example should obviously be interpreted as:

(.method given $x) given $y; # calls $x.method

I think this is similar to how I mentioned that a duplicate 'for' is
pointless. Just because pointless modifier combinations exist doesn't
mean multiple modifiers in general are a problem.


>lowest? why lowest?

Ehm, because that is consistent with current behavior?

>Careful with that.... If you make it a lowest
>precedence operator,
>
> $x = $y if $z; # = is higher precedence
>
>Does it get assigned if $z is undefined?

since 'if' has a lower precedence than '=', this is:
($x = $y) if $z;
or equivalently
$z and ($x = $y)

In either case, the assignment is done if $z is true

>I may be missing something, but
>
> print if $x if $y; # ??
>
>Are you saying "test $y, and if it's true, test $x, and if it's true
>then print"?

Yes

>I suppose that might work....but that still makes it high
>priority, doesn't it?

It means the left side is not always evaluated; that's short-circuiting
and has nothing to do with precedence. Notice how in perl 5 the 'or'
operator is in the lowest precedence class, but certainly short-
circuits (think "foo or die")


> print "$x,$y\n" for $x -> @x for $y -> @y; # is that approximate?

Syntax error. The -> operator doesn't make sense without a block.
See http://www.perl.com/pub/a/2002/10/30/topic.html

>Still,
>
> print for @x for @y; # @y's topic masked
>

>would probably make no sense unless ...

Note that I actually *said* it makes no sense. I have to admit that
if the conditionals (if, unless, when) would be operators, I'd have
trouble to think of a situation where multiple modifiers are useful at
all; which I why I said making the conditionals infix-operators would
probably suffice.

Then again, I just thought up (perl 5 style):

print for split while <>;

but I have to admit I can probably live without the ability to write
something like that ;-)

Paul

unread,
Mar 10, 2003, 2:28:41 PM3/10/03
to Matthijs van Duin, perl6-l...@perl.org
> I made a mistake in my original post, they definitely need to be
> left-associative. Your example should obviously be interpreted as:

>
> (.method given $x) given $y; # calls $x.method

ok.

> I think this is similar to how I mentioned that a duplicate 'for' is
> pointless. Just because pointless modifier combinations exist
> doesn't mean multiple modifiers in general are a problem.

Agreed. But is it worth putting them in if they would make a problem so
easily, and it can be so easily handled with blocks?



> since 'if' has a lower precedence than '=', this is:
> ($x = $y) if $z;
> or equivalently
> $z and ($x = $y)

duh. ok.

> > print if $x if $y; # ??
> >
> >Are you saying "test $y, and if it's true, test $x, and if it's true
> >then print"?
>
> Yes

ok, but wouldn't it be clearer to say

print if $y and $x; # ?



> It means the left side is not always evaluated; that's
> short-circuiting and has nothing to do with precedence.
> Notice how in perl 5 the 'or' operator is in the lowest

> precedence class, but certainly short-circuits (think "foo or die")

But that's easier on the brain, becausewe read left-to-right, and it
short-circuits left-to-right. "z() if $x if $y" doesn't.

> > print "$x,$y\n" for $x -> @x for $y -> @y; # is that approximate?
>
> Syntax error. The -> operator doesn't make sense without a block.
> See http://www.perl.com/pub/a/2002/10/30/topic.html

But if() currently takes a block unless it's postfix.
I was just extrapolating, though as I said, I'd hate to try and write
the parser.



> > print for @x for @y; # @y's topic masked
> >would probably make no sense unless ...
>
> Note that I actually *said* it makes no sense. I have to admit that
> if the conditionals (if, unless, when) would be operators, I'd have
> trouble to think of a situation where multiple modifiers are useful
> at all; which I why I said making the conditionals infix-operators
> would probably suffice.

I was just agreeing there. :)

> Then again, I just thought up (perl 5 style):
>
> print for split while <>;
>
> but I have to admit I can probably live without the ability to write
> something like that ;-)

Ditto....but I have *wanted* to do something vaguely like that on
*several* occasions! More often it is conditionals, though.

I'll leave it to better minds, and use whatever they give me. ;o]

Matthijs Van Duin

unread,
Mar 10, 2003, 2:52:35 PM3/10/03
to perl6-l...@perl.org
On Mon, Mar 10, 2003 at 11:28:41AM -0800, Paul wrote:
>Agreed. But is it worth putting them in if they would make a problem so
>easily, and it can be so easily handled with blocks?

I don't think they can make a problem so easily, and I think it's worth
putting them in because afaics it's not very complicated. Just make 'em
short-circuiting infix operators, like 'and' and 'or', except it's the
left side that's conditionally evaluated rather than the right side.

>> > print if $x if $y; # ??
>> >
>> >Are you saying "test $y, and if it's true, test $x, and if it's true
>> >then print"?
>>
>> Yes
>
>ok, but wouldn't it be clearer to say
>
> print if $y and $x; # ?

I think so. Alternatively you can write
$y and $x and print;

or if 'if' is an infix operator even:
($x if $y) and print;

Personally I think 'print if $y and $x' is the clearest indeed, but maybe
otherwise have a different taste.

To be honest, I doubt it'd be useful to stack multiple R2L short-circuiting
operators, but the ability to do so is obviously there. The main reason I
suggested it was because it means that support for multiple statement
modifiers isn't needed to allow:

.method when MyClass given $obj;

because 'when' would just be an operator here, and 'given' the only
modifier. Another reason to make this change is because 'if' and 'unless'
are in fact just 'and' and 'or' with different precedence in behavior.
Short-circuiting operators already exist, so the only new thing would be
R2L short-circuiting instead of L2R, but I doubt that's a problem.

Thus it reserves the title 'statement modifier' for the heavier stuff:
loops (for, while, until) and topicalizers (given).


>But that's easier on the brain, becausewe read left-to-right, and it
>short-circuits left-to-right. "z() if $x if $y" doesn't.

So don't stack multiple R2L short-circuiting operators if you think
it's confusing; I'm definitely not going to force you to :-)

Seriously, the nice thing is that the behavior of existing use of the
three modifiers remains exactly the same. Sure, you can write a few
new things that are unintelligable, but I think that perl offers rich
opportunities anyway for people who want to write obfuscated code. If
you don't want to obfuscate, then simply don't.

The when/given construction however is a clear example of a useful new
construction that making if/unless/when operators would allow.


>But if() currently takes a block unless it's postfix.
>I was just extrapolating, though as I said, I'd hate to try and write
>the parser.

I don't see the problem.

The 'if' function takes a block. The 'if' infix operator works like
the 'and' operator except it short-circuits the other way around.

There is no conflict here, think about unary '-' versus infix '-'.


To summarize:

PROPOSAL
Replace the 'if', 'unless', 'when' statement modifiers by identically
named lowest-precedence left-associative operators that short-circuit
from right to left.

This means 'FOO if BAR' is identical to 'BAR and FOO', except it has a
lower precedence, and 'FOO unless BAR' is identical to 'BAR or FOO',
except it has a lower precedence. FOO and BAR are arbitrary expressions.
Because of left-associativity, 'FOO if BAR if BAZ' is identical to
'BAZ and BAR and FOO'.

'FOO when BAR' is similar to 'FOO if BAR' except BAR is matched magically
like the rhs of the ~~ operator and an implicit 'break' occurs if true.

RATIONALE
1. it doesn't hurt anything: existing use of the modifiers (now operators)
remains functionally the same.
2. it allows new useful expressions
3. it's more consistent ('if' has no reason being more special than 'and')
4. it shouldn't make parsing more difficult

Luke Palmer

unread,
Mar 10, 2003, 3:14:05 PM3/10/03
to p...@nubz.org, perl6-l...@perl.org
> PROPOSAL
> Replace the 'if', 'unless', 'when' statement modifiers by identically
> named lowest-precedence left-associative operators that short-circuit
> from right to left.
>
> This means 'FOO if BAR' is identical to 'BAR and FOO', except it has a
> lower precedence, and 'FOO unless BAR' is identical to 'BAR or FOO',
> except it has a lower precedence. FOO and BAR are arbitrary expressions.
> Because of left-associativity, 'FOO if BAR if BAZ' is identical to
> 'BAZ and BAR and FOO'.
>
> 'FOO when BAR' is similar to 'FOO if BAR' except BAR is matched magically
> like the rhs of the ~~ operator and an implicit 'break' occurs if true.
>
> RATIONALE
> 1. it doesn't hurt anything: existing use of the modifiers (now operators)
> remains functionally the same.
> 2. it allows new useful expressions
> 3. it's more consistent ('if' has no reason being more special than 'and')
> 4. it shouldn't make parsing more difficult

It is nice to see someone who puts as much thought into posting as you
do. Unfortunately, your proposal is moot, as we have a definitive
"No, still can't chain them" from Larry.

http://archive.develooper.com/perl6-language%40perl.org/msg09331.html

Luke

Matthijs Van Duin

unread,
Mar 10, 2003, 3:54:20 PM3/10/03
to perl6-l...@perl.org
On Mon, Mar 10, 2003 at 01:14:05PM -0700, Luke Palmer wrote:
>It is nice to see someone who puts as much thought into posting as you
>do. Unfortunately, your proposal is moot, as we have a definitive
>"No, still can't chain them" from Larry.
>
> http://archive.develooper.com/perl6-language%40perl.org/msg09331.html

Thanks for the reference

However, I'm pretty sure he's talking about disallowing multiple modifiers
there. My "if/unless/when as operator" proposal is exactly to avoid having
to support multiple modifiers. So I think discussion on this might still
be fruitful.

I'm actually a bit surprised noone had the idea earlier; to me the if-
modifier is so similar to the 'and'-operator to have no reason being
a modifier (it's actually implemented using 'and' internally in p5).

Contrast that to the 'for'-modifier, which really has an impact on the
statement unlike any operator could achieve, and hence really needs to
be a statement modifier.

Simon Cozens

unread,
Mar 11, 2003, 4:28:22 AM3/11/03
to perl6-l...@perl.org
fibo...@babylonia.flatirons.org (Luke Palmer) writes:
> we have a definitive
^^^^^^^^^^
Remember that this is Perl 6. You keep using that word, etc.

--
void russian_roulette(void) { char *target; strcpy(target, "bullet"); }

Brent Dax

unread,
Mar 11, 2003, 11:46:05 AM3/11/03
to Simon Cozens, perl6-l...@perl.org
Simon Cozens:
# fibo...@babylonia.flatirons.org (Luke Palmer) writes:
# > we have a definitive
# ^^^^^^^^^^
# Remember that this is Perl 6. You keep using that word, etc.

It *is* definitive, Simon...at least this week. ;^)

--Brent Dax <bren...@cpan.org>
@roles=map {"Parrot $_"} qw(embedding regexen Configure)

>How do you "test" this 'God' to "prove" it is who it says it is?
"If you're God, you know exactly what it would take to convince me. Do
that."
--Marc Fleury on alt.atheism

0 new messages