Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

|| && and or cannot be "overloaded" #5997

Open
p6rt opened this issue Jan 11, 2017 · 9 comments
Open

|| && and or cannot be "overloaded" #5997

p6rt opened this issue Jan 11, 2017 · 9 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Jan 11, 2017

Migrated from rt.perl.org#130540 (status was 'open')

Searchable as RT130540$

@p6rt
Copy link
Author

p6rt commented Jan 11, 2017

From @FCO

If I write another || operator it will continue to use the original version.

https://irclog.perlgeek.de/perl6/2017-01-10#i_13895823 <https://irclog.perlgeek.de/perl6/2017-01-10#i_13895823>

@p6rt
Copy link
Author

p6rt commented Jan 11, 2017

From @zoffixznet

On Tue, 10 Jan 2017 16​:23​:18 -0800, fernandocorrea@​gmail.com wrote​:

If I write another || operator it will continue to use the original
version.

https://irclog.perlgeek.de/perl6/2017-01-10#i_13895823
<https://irclog.perlgeek.de/perl6/2017-01-10#i_13895823>

To save other readers sifting through the chan log... Even an only sub doesn't take root​:

  <Zoffix> m​: sub infix​:<||> ($, $) {"hi"}; say 42 || 55
  <camelia> rakudo-moar 9a11ea​: OUTPUT«42␤»

This applies to &&, and, or, and I'd guess any shortcurcuiting operator.

@p6rt
Copy link
Author

p6rt commented Jan 11, 2017

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Jan 11, 2017

From @jnthn

On Tue, 10 Jan 2017 17​:59​:05 -0800, cpan@​zoffix.com wrote​:

On Tue, 10 Jan 2017 16​:23​:18 -0800, fernandocorrea@​gmail.com wrote​:

If I write another || operator it will continue to use the original
version.

https://irclog.perlgeek.de/perl6/2017-01-10#i_13895823
<https://irclog.perlgeek.de/perl6/2017-01-10#i_13895823>

To save other readers sifting through the chan log... Even an only sub
doesn't take root​:

<Zoffix> m​: sub infix​:<||> ($, $) {"hi"}; say 42 || 55
<camelia> rakudo-moar 9a11ea​: OUTPUT«42␤»

This applies to &&, and, or, and I'd guess any shortcurcuiting
operator.

These are special compiler forms that receive special code-gen, due to their shortcircuiting nature, and so do not result in sub calls. Thus there's no sub to override.

@p6rt
Copy link
Author

p6rt commented Jan 12, 2017

From @LLFourn

There is a &infix​:<&&> which might be where some confusion comes from. I
guess that's there for meta operators. For example​:

multi sub infix​:<&&>("foo","bar") { "win" };
say "foo" && "bar" # bar
say <foo> Z&& <bar> # win

so it does kinda work actually just not as you might expect.

LL

On Thu, Jan 12, 2017 at 9​:21 AM jnthn@​jnthn.net via RT <
perl6-bugs-followup@​perl.org> wrote​:

On Tue, 10 Jan 2017 17​:59​:05 -0800, cpan@​zoffix.com wrote​:

On Tue, 10 Jan 2017 16​:23​:18 -0800, fernandocorrea@​gmail.com wrote​:

If I write another || operator it will continue to use the original
version.

https://irclog.perlgeek.de/perl6/2017-01-10#i_13895823
<https://irclog.perlgeek.de/perl6/2017-01-10#i_13895823>

To save other readers sifting through the chan log... Even an only sub
doesn't take root​:

<Zoffix> m​: sub infix​:<||> ($, $) {"hi"}; say 42 || 55
<camelia> rakudo-moar 9a11ea​: OUTPUT«42␤»

This applies to &&, and, or, and I'd guess any shortcurcuiting
operator.

These are special compiler forms that receive special code-gen, due to
their shortcircuiting nature, and so do not result in sub calls. Thus
there's no sub to override.

@p6rt
Copy link
Author

p6rt commented Jan 12, 2017

From @lizmat

[10​:04​:44] <lizmat> re the shortcircuiting behaviour of && || and friends, and the fact you cannot override them (RT #​130540)
[10​:05​:09] <lizmat> I wonder whether we perhaps need a new kind of signature that would make this work​:
[10​:05​:20] <lizmat> sub a(*@​a) { dd @​a.shift }; my $a = 0; a $a++,$a++; dd $a # sorta expected to see $a = 1 here
[10​:05​:27] <lizmat> m​: sub a(*@​a) { dd @​a.shift }; my $a = 0; a $a++,$a++; dd $a
[10​:05​:27] <camelia> rakudo-moar 8f3476​: OUTPUT«0␤Int $a = 2␤»
[10​:05​:52] <lizmat> aka, a lazy slurpy signature
[10​:06​:41] <lizmat> which would allow && || and friends to become full HLL citizens, rather then needing to be handled at codegen level and thus make them unoverridable

On 12 Jan 2017, at 06​:28, Lloyd Fournier <lloyd.fourn@​gmail.com> wrote​:

There is a &infix​:<&&> which might be where some confusion comes from. I guess that's there for meta operators. For example​:
multi sub infix​:<&&>("foo","bar") { "win" };
say "foo" && "bar" # bar
say <foo> Z&& <bar> # win

so it does kinda work actually just not as you might expect.

LL

On Thu, Jan 12, 2017 at 9​:21 AM jnthn@​jnthn.net via RT <perl6-bugs-followup@​perl.org> wrote​:
On Tue, 10 Jan 2017 17​:59​:05 -0800, cpan@​zoffix.com wrote​:

On Tue, 10 Jan 2017 16​:23​:18 -0800, fernandocorrea@​gmail.com wrote​:

If I write another || operator it will continue to use the original
version.

https://irclog.perlgeek.de/perl6/2017-01-10#i_13895823
<https://irclog.perlgeek.de/perl6/2017-01-10#i_13895823>

To save other readers sifting through the chan log... Even an only sub
doesn't take root​:

<Zoffix> m​: sub infix​:<||> ($, $) {"hi"}; say 42 || 55
<camelia> rakudo-moar 9a11ea​: OUTPUT«42␤»

This applies to &&, and, or, and I'd guess any shortcurcuiting
operator.

These are special compiler forms that receive special code-gen, due to their shortcircuiting nature, and so do not result in sub calls. Thus there's no sub to override.

@p6rt
Copy link
Author

p6rt commented Jan 12, 2017

From @smls

@​lizmat​:

Aren't signatures that affect how a function call is parsed/compiled (a la Perl 5 "prototypes"), something that Perl 6 intentionally moved away from?

Regarding the "thunky" operators like ||, &&, xx​:

My conceptual view of them, is that each of them exists simultaneously as a macro (that offers the short-circuiting/lazy-evaluation behavior) and as function (which accepts Perl 6 objects rather than thunks).

The macro clobbers the function, so when the operator is encountered normally in the source code, the compiler (conceptually) invokes the macro, thus generating code for the lazy evaluation behavior.

But you actually *can* get at the function version as well, which obviously can't be short-circuiting/lazy​:

  ➜ say &[||]
  sub infix​:<||> (| is raw) { #`(Sub+{Precedence}|64299168) ... }
  ➜ say &[||](0, 5)
  5
  ➜ say &[||]( 1, say("Hi") )
  Hi
  1

In this view, adding a multi candidate to the function does nothing to override the macro, so this wouldn't be considered a bug.

Rather, the solution would be to provide sufficiently flexible macro functionality to allow users to override or extend these built-in macros.

@p6rt
Copy link
Author

p6rt commented Jan 12, 2017

From @FCO

But should those 2 forms behave differently?

20​:40 <SmokeMachine> m​: multi infix​:<||>(42, 42) {"OK"}; say infix​:<||>(42, 42); say 42 || 42
20​:40 <camelia> rakudo-moar 8f3476​: OUTPUT«OK␤42␤»

Enviado do meu iPhone

Em 11 de jan de 2017, às 20​:20, jnthn@​jnthn.net via RT <perl6-bugs-followup@​perl.org> escreveu​:

On Tue, 10 Jan 2017 17​:59​:05 -0800, cpan@​zoffix.com wrote​:

On Tue, 10 Jan 2017 16​:23​:18 -0800, fernandocorrea@​gmail.com wrote​:
If I write another || operator it will continue to use the original
version.

https://irclog.perlgeek.de/perl6/2017-01-10#i_13895823
<https://irclog.perlgeek.de/perl6/2017-01-10#i_13895823>

To save other readers sifting through the chan log... Even an only sub
doesn't take root​:

<Zoffix> m​: sub infix​:<||> ($, $) {"hi"}; say 42 || 55
<camelia> rakudo-moar 9a11ea​: OUTPUT«42␤»

This applies to &&, and, or, and I'd guess any shortcurcuiting
operator.

These are special compiler forms that receive special code-gen, due to their shortcircuiting nature, and so do not result in sub calls. Thus there's no sub to override.

@p6rt
Copy link
Author

p6rt commented Jan 12, 2017

From @bdw

I'm not sure if this adds anything, but​:
The reason why infix<||> doesn't work as you'd expect, is that operators
like '||' and '&&' operate on expressions, that is to say code, rather than
on values.
The only valid way to extend such operators is to either use macros (which
would receive both sides as arguments) or to create 'thunks' out of both
sides.
Either way, a sub that takes value arguments is incompatible with the
short-circuiting semantics, and I think it should fail at compile time.

Regards,
Bart

2017-01-11 23​:42 GMT+01​:00 Fernando Oliveira <fernandocorrea@​gmail.com>​:

But should those 2 forms behave differently?

20​:40 <SmokeMachine> m​: multi infix​:<||>(42, 42) {"OK"}; say
infix​:<||>(42, 42); say 42 || 42
20​:40 <camelia> rakudo-moar 8f3476​: OUTPUT«OK␤42␤»

Enviado do meu iPhone

Em 11 de jan de 2017, às 20​:20, jnthn@​jnthn.net via RT <
perl6-bugs-followup@​perl.org> escreveu​:

On Tue, 10 Jan 2017 17​:59​:05 -0800, cpan@​zoffix.com wrote​:

On Tue, 10 Jan 2017 16​:23​:18 -0800, fernandocorrea@​gmail.com wrote​:
If I write another || operator it will continue to use the original
version.

https://irclog.perlgeek.de/perl6/2017-01-10#i_13895823
<https://irclog.perlgeek.de/perl6/2017-01-10#i_13895823>

To save other readers sifting through the chan log... Even an only sub
doesn't take root​:

<Zoffix> m​: sub infix​:<||> ($, $) {"hi"}; say 42 || 55
<camelia> rakudo-moar 9a11ea​: OUTPUT«42␤»

This applies to &&, and, or, and I'd guess any shortcurcuiting
operator.

These are special compiler forms that receive special code-gen, due to
their shortcircuiting nature, and so do not result in sub calls. Thus
there's no sub to override.

@p6rt p6rt added the Bug label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant