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

Proposal: split ternary ?? :: into binary ?? and //

0 views
Skip to first unread message

Thomas Sandlass

unread,
Sep 5, 2005, 8:38:58 AM9/5/05
to perl6-l...@perl.org
HaloO,

I'm still contemplating how to get rid of the :: in the
ternary and make :: unequivocally available for a type
sigil and as a binary infix for symbol lookup.

Here's a possible solution:

1) ?? becomes a binary operator that behaves as follows:
a) it evaluates its lhs in boolean context
b) if this is true, ?? evaluates its rhs such that it
can't be undef
c) if this is false, ?? returns undef

2) the precedence of ?? is above // which naturally catches
the undef of a false condition.

This gives

$x = $y < 3 ?? "small" // "large";

and possibly even a low precedence version

$x = $y < 3 then "small" err "large"; # hmm
thence # old fashioned
therefore # long, but clear

An implementation could read:

&*infix:<??> (&cond, &code)
is parsed(...) # trait needed to insert {}?
{
if &cond() { return &code() but defined }
else { return undef }
}


Comments?

TSa

Juerd

unread,
Sep 5, 2005, 3:58:23 PM9/5/05
to Thomas Sandlass, perl6-l...@perl.org
Thomas Sandlass skribis 2005-09-05 14:38 (+0200):

> b) if this is true, ?? evaluates its rhs such that it
> can't be undef

But

$foo ?? undef // 1

then is a problem.


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

Damian Conway

unread,
Sep 5, 2005, 5:26:37 PM9/5/05
to perl6-l...@perl.org
Thomas Sandlass wrote:

> I'm still contemplating how to get rid of the :: in the
> ternary
>

> Comments?
I believe that the single most important feature of the ternary operator is
that it is ternary. That is, unlike an if-else sequence, it's impossible to
leave out the "else" in a ternary operation. Splitting the ternary destroys
that vital characteristic, which would be a very bad outcome.

Damian

Damian Conway

unread,
Sep 5, 2005, 6:18:16 PM9/5/05
to Patrick R. Michaud, perl6-l...@perl.org
Patrick suggested:

> At OSCON I was also thinking that it'd be really nice to get rid of
> the :: in the ternary and it occurred to me that perhaps we could use
> something like '?:' as the 'else' token instead:
>
> (cond) ?? (if_true) ?: (if_false)
>
> However, I'll freely admit that I hadn't investigated much further
> to see if this might cause other syntax ambiguities.

I think the main problem there would be the *visual* similarity
between the two.

Damian

Ashley Winters

unread,
Sep 5, 2005, 9:00:03 PM9/5/05
to perl6-l...@perl.org

Indeed. The "logical" (bad pun intended) operator to match with ?? is !!

(cond) ?? (if_true) !! (if_false)

Ashley Winters

Luke Palmer

unread,
Sep 5, 2005, 10:52:16 PM9/5/05
to Juerd, Thomas Sandlass, perl6-l...@perl.org
On 9/5/05, Juerd <ju...@convolution.nl> wrote:
> Thomas Sandlass skribis 2005-09-05 14:38 (+0200):
> > b) if this is true, ?? evaluates its rhs such that it
> > can't be undef
>
> But
>
> $foo ?? undef // 1
>
> then is a problem.

Yeah. Hmm, but I kinda like the look of ?? //, and I don't like the
overloading of :: in that way anymore. So it's possible just to add
a ternary ?? // in addition to, and unrelated to (from the parser's
perspective), the regular //.

?? !! ain't bad either.

Luke

Damian Conway

unread,
Sep 6, 2005, 1:33:27 AM9/6/05
to perl6-l...@perl.org
Luke wrote:

> Yeah. Hmm, but I kinda like the look of ?? //, and I don't like the
> overloading of :: in that way anymore. So it's possible just to add
> a ternary ?? // in addition to, and unrelated to (from the parser's
> perspective), the regular //.

Bad idea. This useful construct would then be ambiguous:

$val = some_cond()
?? $arg1 // $arg1_default
// $arg2 // $arg2_default;


> ?? !! ain't bad either.

It's definitely much better that sabotaging the (highly useful) // operator
within (highly useful) ternaries.

Damian


Luke Palmer

unread,
Sep 6, 2005, 1:38:50 AM9/6/05
to Damian Conway, perl6-l...@perl.org
On 9/6/05, Damian Conway <dam...@conway.org> wrote:
> Luke wrote:
>
> > Yeah. Hmm, but I kinda like the look of ?? //, and I don't like the
> > overloading of :: in that way anymore. So it's possible just to add
> > a ternary ?? // in addition to, and unrelated to (from the parser's
> > perspective), the regular //.
>
> Bad idea. This useful construct would then be ambiguous:
>
> $val = some_cond()
> ?? $arg1 // $arg1_default
> // $arg2 // $arg2_default;

Huh, yeah. We'd have to go one way or the other on that, and neither
of those are what you intend.

Not that being explicit is always a bad thing:

$val = some_cond()
?? ($arg1 // $arg1_default)
// ($arg2 // $arg2_default)

And I question your notion of "highly useful" in this case. Still, it
is probably linguistically not a good idea to overload // like that.

>
> > ?? !! ain't bad either.
>
> It's definitely much better that sabotaging the (highly useful) // operator
> within (highly useful) ternaries.

I guess the thing that I really think is nice is getting :: out of
that role and into the type-only domain.

Luke

Damian Conway

unread,
Sep 6, 2005, 2:07:23 AM9/6/05
to perl6-l...@perl.org
Luke wrote:

> Not that being explicit is always a bad thing:
>
> $val = some_cond()
> ?? ($arg1 // $arg1_default)
> // ($arg2 // $arg2_default)

No. What's a bad thing is creating new linguistic traps for when people
inevitably forget to be explicit.


> And I question your notion of "highly useful" in this case.

Huh??? Both // and the ternary are unquestionably highly useful constructs. My
point was that making them not work together is a Very Bad Idea.


> I guess the thing that I really think is nice is getting :: out of
> that role and into the type-only domain.

Yes, I'd certainly like that too. But not at the cost of turning // into a
bear-pit for the unsuspecting.

Damian

Patrick R. Michaud

unread,
Sep 5, 2005, 5:51:02 PM9/5/05
to Damian Conway, perl6-l...@perl.org

At OSCON I was also thinking that it'd be really nice to get rid of

the :: in the ternary and it occurred to me that perhaps we could use
something like '?:' as the 'else' token instead:

(cond) ?? (if_true) ?: (if_false)

However, I'll freely admit that I hadn't investigated much further
to see if this might cause other syntax ambiguities.

Pm

Thomas Sandlass

unread,
Sep 6, 2005, 7:49:40 AM9/6/05
to perl6-l...@perl.org
HaloO,

Luke wrote:
> > > ?? !! ain't bad either.
> >
> > It's definitely much better that sabotaging the
> > (highly useful) // operator
> > within (highly useful) ternaries.
>

> I guess the thing that I really think is nice is getting :: out of
> that role and into the type-only domain.

Right. To make :: indicate type or meta was my primary concern.
So I see the following situation:

unwanted
?? ::

ASCII replacements
?? // # two binaries
?? \\ # I would like it as chaining binary nor
?? !! # wasn't binary ! the none() constructor
# and !! the binary nor---at least in Pugs?

Latin1 replacements
?? ¦¦
?? ¡¡
?? ¿¿

@Larry's choice?
--
TSa

arcadi....@gmail.com

unread,
Sep 6, 2005, 8:44:35 AM9/6/05
to
now that a pair ( ... => ... ) can have any type for key and value we
can have define a binary

&*infix:<??> (&cond, Pair)

The trinary ?? :: will be imitated as

?? ... => ...

$x = $y < 3 ?? "small" => "large";

such ?? may be useful on its own as it impose Pair context on its
second argument.

Luke Palmer

unread,
Sep 6, 2005, 8:09:54 AM9/6/05
to Thomas Sandlass, perl6-l...@perl.org
On 9/6/05, Thomas Sandlass <Thomas....@barco.com> wrote:
> Right. To make :: indicate type or meta was my primary concern.

Okay, now why don't you tell us about this new binary :: you're proposing.

Luke

Thomas Sandlass

unread,
Sep 6, 2005, 10:57:30 AM9/6/05
to perl6-l...@perl.org
HaloO,

Luke wrote:
> Okay, now why don't you tell us about this new binary :: you're proposing.

Well, not a new one. Just plain old foo::bar::blahh and 'my ::blubb $x'
with relaxed whitespace rules. The ternary ?? :: is a splinter in my
mind's eye because it is not a compile time or symbol lookup construct.

The driving idea is to let tokens always mean the same or at least very
similar things in different contexts. And I thought that is your rating
as well. For :: that should be 'symbol table management'. E.g. ::= fits
that notion perfectly while the alternative separation of the ternary
doesn't.

There's yet another approach, to make ternary listfix:

$val = $cond ?? "true" ?? "false";

and perhaps we change it from ternary to (n > 2)-ary:

$val = $tested_only ?? $val_or_test ?? $other_or_test ?? $final;

But in both ways, recursive "ternary" needs parens.
Which can be regarded as a good thing. This listfix ??
behaves more like a rhs returning ||. As the RegularityGuy(TM)
I like the chaining triple // || \\ where // returns the first
defined, || returns the first true and \\ returns the first false.
And of course there are low precedence counterparts err, or, nor.
And the binary \ for none() construction. BTW is (\) indicating
the relative complement of the set operators? Even the unary ref
prefix makes sense as \ because it means 'not the value' or
'not right here but keep for later reference'.
--
TSa


Nicholas Clark

unread,
Sep 6, 2005, 12:14:15 PM9/6/05
to Thomas Sandlass, perl6-l...@perl.org
On Tue, Sep 06, 2005 at 04:57:30PM +0200, Thomas Sandlass wrote:

> There's yet another approach, to make ternary listfix:
>
> $val = $cond ?? "true" ?? "false";

So
^^ that one
doesn't do the same thing as
^^ that one?

I'd find that confusing in itself.
And that's without considering the change from the C<? :> of C and Perl 5.

[contents of this message may settle in transit, particularly if your mail
reader uses a proportional font.]

Nicholas Clark

Larry Wall

unread,
Sep 7, 2005, 11:32:39 AM9/7/05
to perl6-l...@perl.org
On Tue, Sep 06, 2005 at 04:57:30PM +0200, Thomas Sandlass wrote:
: HaloO,

:
: Luke wrote:
: > Okay, now why don't you tell us about this new binary :: you're proposing.
:
: Well, not a new one. Just plain old foo::bar::blahh and 'my ::blubb $x'
: with relaxed whitespace rules. The ternary ?? :: is a splinter in my
: mind's eye because it is not a compile time or symbol lookup construct.
:
: The driving idea is to let tokens always mean the same or at least very
: similar things in different contexts. And I thought that is your rating
: as well. For :: that should be 'symbol table management'. E.g. ::= fits
: that notion perfectly while the alternative separation of the ternary
: doesn't.

I think that's a powerful argument even if we don't have an infix:<::>.
Plus I hate all infix "nor" operators due to my English-speaking bias
that requires a "neither" on the front. So let's go ahead and make
it ??!!. (At least this week...)

Larry

Patrick R. Michaud

unread,
Sep 7, 2005, 11:37:50 AM9/7/05
to perl6-l...@perl.org
On Wed, Sep 07, 2005 at 08:32:39AM -0700, Larry Wall wrote:
>
> I think that's a powerful argument even if we don't have an infix:<::>.
> Plus I hate all infix "nor" operators due to my English-speaking bias
> that requires a "neither" on the front. So let's go ahead and make
> it ??!!. (At least this week...)

Yay !!

Pm

Juerd

unread,
Sep 7, 2005, 1:08:40 PM9/7/05
to perl6-l...@perl.org
Larry Wall skribis 2005-09-07 8:32 (-0700):

> I think that's a powerful argument even if we don't have an infix:<::>.
> Plus I hate all infix "nor" operators due to my English-speaking bias
> that requires a "neither" on the front. So let's go ahead and make
> it ??!!. (At least this week...)

I was going to object based on !!foo being ! ! foo, but that !! is
spelled ? now, so nevermind.

Simon Cozens

unread,
Sep 8, 2005, 7:18:12 PM9/8/05
to perl6-l...@perl.org
la...@wall.org (Larry Wall) writes:
> So let's go ahead and make it ??!!. (At least this week...)

I hereby christen this "the interrobang operator".
(http://en.wikipedia.org/wiki/Interrobang)

--
"Your fault: core dumped"
-- MegaHAL

0 new messages