Shortcut: ?=

0 views
Skip to first unread message

Miko O'Sullivan

unread,
Feb 2, 2003, 8:49:51 PM2/2/03
to perl6-l...@perl.org
SUMMARY

C<$var ?= $x : $y> as a shortcut for C<$var = $var ? $x : $y>.


DETAILS

We have ||=, +=, -=, etc. These shortcuts (I'm sure there's some fancy
linguistic term for them) save us a few keystrokes and clean up the code.

So, concerning C<? :>, I find myself doing this type of thing a lot:

$var = $var ? 1 : 0;

How 'bout a shortcut for that, something like this:

$var ?= 1 : 0;


-miko

Miko O'Sullivan
Programmer Analyst
Rescue Mission of Roanoke

Mr. Nobody

unread,
Feb 2, 2003, 9:15:36 PM2/2/03
to perl6-l...@perl.org

Good idea, but ? and : were renamed to ?? and :: in perl6, so it should be ??=

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com

Deborah Ariel Pickett

unread,
Feb 2, 2003, 9:57:18 PM2/2/03
to Miko O'Sullivan, perl6-l...@perl.org
> SUMMARY
> C<$var ?= $x : $y> as a shortcut for C<$var = $var ? $x : $y>.
>
>
> DETAILS
> We have ||=, +=, -=, etc. These shortcuts (I'm sure there's some fancy
> linguistic term for them) save us a few keystrokes and clean up the code.
>
> So, concerning C<? :>, I find myself doing this type of thing a lot:
> $var = $var ? 1 : 0;
> How 'bout a shortcut for that, something like this:
> $var ?= 1 : 0;

[Modulo already-pointed-out Perl6 operator spelling changes.]

I agree that it would seem right to have a ??= assignment operator,
since a bunch of other operators have it too. If there's no added
complexity to the Perl parser in introducing this operator, there's
probably no argument against it.

But . . . (metaphysical waffle begins)

What argument is there *for* including a ??= operator? The whole point
in the assignment operators +=, ||=, //=, etc. is that they modify the
*existing value* of the variable. That is, the final result of the
variable usually reflects in some way the original value.

The example above:
$var ??= 1 :: 0;
would end up with either 1 or 0 in $var, pretty much irrespective of the
value that was in $var. At least these:
$var ||= 1;
$var &&= 0;
leave the possibility that the original value of $var is retained. A
??= operator wouldn't be in the spirit of the other assignment
operators.

(Well, yes, there's:
$var ??= $var :: $foo;
and the like, but those can equally be expressed in terms of operators
we already have, for instance:
$var ||= $foo;
)

I guess what I'm saying is that someone needs to provide a real-world,
non-contrived, example showing ??= in use. This example would need to
demonstrate that using ||= or &&= or its relatives would be less
comprehensible.

(metaphysical waffle ends)

I can't imagine ever needing such an operator. Perhaps that's hubris
(in which case, prove me wrong with an example), or perhaps it's because
there *is* no need for such an operator (in which case all it'll be is
another footnote in the Camel nth edition). Which is it?

--
Debbie Pickett http://www.csse.monash.edu.au/~debbiep deb...@csse.monash.edu.au
"You gotta bat your eyes - like this." - _The Little Mermaid_

Joseph F. Ryan

unread,
Feb 2, 2003, 10:10:41 PM2/2/03
to Miko O'Sullivan, perl6-l...@perl.org
Miko O'Sullivan wrote:

>SUMMARY
>
>C<$var ?= $x : $y> as a shortcut for C<$var = $var ? $x : $y>.
>
>
>DETAILS
>
>We have ||=, +=, -=, etc. These shortcuts (I'm sure there's some fancy
>linguistic term for them) save us a few keystrokes and clean up the code.
>
>So, concerning C<? :>, I find myself doing this type of thing a lot:
>
> $var = $var ? 1 : 0;
>
>How 'bout a shortcut for that, something like this:
>
> $var ?= 1 : 0;
>
>
>-miko
>

Doesn't the perl6 //= operator already do what you suggest?


Joseph F. Ryan
ryan...@osu.edu


Austin Hastings

unread,
Feb 3, 2003, 9:25:09 AM2/3/03
to Miko O'Sullivan, perl6-l...@perl.org

--- Miko O'Sullivan <mi...@idocs.com> wrote:
> SUMMARY
>
> C<$var ?= $x : $y> as a shortcut for C<$var = $var ? $x : $y>.
>
>
> DETAILS
>
> We have ||=, +=, -=, etc. These shortcuts (I'm sure there's some
> fancy
> linguistic term for them) save us a few keystrokes and clean up the
> code.
>
> So, concerning C<? :>, I find myself doing this type of thing a lot:
>
> $var = $var ? 1 : 0;

The only time this doesn't change type (arguably a bad thing in its own
right) is when you're doing boolean ops. And for those, there exist
boolean operators.

> How 'bout a shortcut for that, something like this:
>
> $var ?= 1 : 0;

Isn't this the same as C<$var &&= 1;> ?

=Austin

Dave Mitchell

unread,
Feb 3, 2003, 9:30:55 AM2/3/03
to Austin Hastings, Miko O'Sullivan, perl6-l...@perl.org
On Mon, Feb 03, 2003 at 06:25:09AM -0800, Austin Hastings wrote:
> The only time this doesn't change type (arguably a bad thing in its own
> right) is when you're doing boolean ops. And for those, there exist
> boolean operators.

Changing type is a very Perlish thing to do.

> > How 'bout a shortcut for that, something like this:
> >
> > $var ?= 1 : 0;
>
> Isn't this the same as C<$var &&= 1;> ?

No

for example, "" maps to 0.

A better example:

$var ??= 'succeeded' :: 'failed';

--
"You're so sadly neglected, and often ignored.
A poor second to Belgium, When going abroad."
Monty Python - "Finland"

Miko O'Sullivan

unread,
Feb 3, 2003, 10:17:20 AM2/3/03
to perl6-l...@perl.org
On Mon, 3 Feb 2003, Deborah Ariel Pickett wrote:

> I guess what I'm saying is that someone needs to provide a real-world,
> non-contrived, example showing ??= in use.

Fair enough. Real World, Non-Contrived: In all databases that I've ever
worked with there are exactly two possible values for a boolean database
field. Those two values are usually 't' and 'f', though in my designs I
prefer to use '1' and '0', to keep things more perlish.

Now, when a value comes in from a web page, it might be anything. In
particular a checkbox is going to be either undef (if the checkbox wasn't
checked, the standard is that the field isn't sent at all) or whatever the
<INPUT> tag's VALUE attribute is set to, or 'on' (if VALUE isn't set).
There is simply no way to directly get 'f' or 0 from the HTTP request, and
't' requires extra work. Ergo, it's necessary to massage the data a
little to fit it into 't' and 'f'.

In short, although Perl is quite robust about what's "true" and "false",
other computer systems are more fussy. I personally find myself
explicitly setting variables to 1 or 0 quite frequently, and I always use
?:. It would be a nice little shorthand addition to have ??= in Perl6.

Obviously this isn't a major requirement. It's just a nice little
shortcut that would clean the code in the same way the other shortcuts do.
I always feel somehow redundant type C<$var = $var ? 1 : 0>, and ??= would
just be nice and tidy.

Sean O'Rourke

unread,
Feb 3, 2003, 11:13:58 AM2/3/03
to Dave Mitchell, Austin Hastings, Miko O'Sullivan, perl6-l...@perl.org
On Mon, 3 Feb 2003, Dave Mitchell wrote:
> $var ??= 'succeeded' :: 'failed';

Aha!

$var && 'succeeded' || 'failed';

Thank you, precedence.

/s

Sean O'Rourke

unread,
Feb 3, 2003, 11:17:41 AM2/3/03
to Dave Mitchell, Austin Hastings, Miko O'Sullivan, perl6-l...@perl.org
Argh. Please disregard that last message as the ramblings of a
pre-caffeinated mind.

/s

Deborah Ariel Pickett

unread,
Feb 3, 2003, 7:41:09 PM2/3/03
to Miko O'Sullivan, perl6-l...@perl.org
> > I guess what I'm saying is that someone needs to provide a real-world,
> > non-contrived, example showing ??= in use.
> Fair enough. Real World, Non-Contrived: In all databases that I've ever
> worked with there are exactly two possible values for a boolean database
> field. Those two values are usually 't' and 'f', though in my designs I
> prefer to use '1' and '0', to keep things more perlish.
> Now, when a value comes in from a web page, it might be anything. In
> particular a checkbox is going to be either undef (if the checkbox wasn't
> checked, the standard is that the field isn't sent at all) or whatever the
> <INPUT> tag's VALUE attribute is set to, or 'on' (if VALUE isn't set).
> There is simply no way to directly get 'f' or 0 from the HTTP request, and
> 't' requires extra work. Ergo, it's necessary to massage the data a
> little to fit it into 't' and 'f'.
> In short, although Perl is quite robust about what's "true" and "false",
> other computer systems are more fussy. I personally find myself
> explicitly setting variables to 1 or 0 quite frequently, and I always use
> ?:. It would be a nice little shorthand addition to have ??= in Perl6.

All right, I'm prepared to buy that argument. (Though I probably still
wouldn't ever use the operator personally.)

I presume you'll summarize this thread to the new area you announced
today, since it seems that there aren't any strong objections. You
should address these issues too, just to make it complete:
- operator precedence and associativity
- issues with typed variables
- how it works with aggregates like arrays and hashes
- whether there's a penguin^H^H^H^H^H^H^Hguillemo^Het hyper-op version
of this, and how it looks (e.g., is the :: also bracketed?) and works.
- context
Some of these could be palmed off by saying "The same as ?? ::", but we
may as well be complete.

John Williams

unread,
Feb 3, 2003, 11:34:32 AM2/3/03
to Miko O'Sullivan, Sean O'Rourke, perl6-l...@perl.org
What about:

($var &&= 'succeeded') ||= 'failed';

Reply all
Reply to author
Forward
0 new messages