Re: [PHP-DEV] [RFC] switch expression

2020-04-02 Thread Terje Slettebø
Larry Garfield wrote: On Sun, Mar 29, 2020, at 4:04 PM, Ilija Tovilo wrote: What you're proposing is a language construct for an *expression*, which evaluates depending on internal logic to a different value. Those are sufficiently distinct that I agree they should have distinct keywords.

Re: [PHP-DEV] [RFC] switch expression

2020-03-30 Thread Ilija Tovilo
Hi Larry > Not everything has to be a statement. This is not what I meant. What I meant is that it should be usable in a statement context. Since you mentioned Rust, match is always an expression. But you can use it without making use of the return value. This is what I'm suggesting. In PHP this

Re: [PHP-DEV] [RFC] switch expression

2020-03-30 Thread Larry Garfield
On Sun, Mar 29, 2020, at 5:07 PM, Ilija Tovilo wrote: > Hi Larry > > Thanks for your suggestion. > > I chose to use switch instead of match for a couple of reasons: > > 1. It uses the same AST, code generation and opcodes as the switch, I > don't agree that it is significantly different than

Re: [PHP-DEV] [RFC] switch expression

2020-03-29 Thread Stanislav Malyshev
Hi! > 1. It uses the same AST, code generation and opcodes as the switch, I > don't agree that it is significantly different than the switch that we > already have. This is reasoning in wrong direction. Nobody cares which opcode it uses. It is significantly different *for the user*, the fact

Re: [PHP-DEV] [RFC] switch expression

2020-03-29 Thread Ilija Tovilo
Hi Larry Thanks for your suggestion. I chose to use switch instead of match for a couple of reasons: 1. It uses the same AST, code generation and opcodes as the switch, I don't agree that it is significantly different than the switch that we already have. 2. Adding the `match` keyword is a

Re: [PHP-DEV] [RFC] switch expression

2020-03-29 Thread G. P. B.
On Sun, 29 Mar 2020 at 23:54, Stanislav Malyshev wrote: > Hi! > > > My recommendation would be to just borrow Rust's keyword: > > > > $result = match ($var) { > > $expression => $expression; > > $expression => $expression; > > $expression => $expression; > > default => $expression; > > }

Re: [PHP-DEV] [RFC] switch expression

2020-03-29 Thread Stanislav Malyshev
Hi! > My recommendation would be to just borrow Rust's keyword: > > $result = match ($var) { > $expression => $expression; > $expression => $expression; > $expression => $expression; > default => $expression; > } Yes, this would be much better idea. -- Stas Malyshev

Re: [PHP-DEV] [RFC] switch expression

2020-03-29 Thread Larry Garfield
On Sun, Mar 29, 2020, at 4:04 PM, Ilija Tovilo wrote: > > Having two syntaxes for one keyword is not a good idea, > > We're already doing that. What about classes vs anonymous objects? > Functions vs closures? > They're using the same keywords. There's no confusion. > > Ilija There's subtle and

Re: [PHP-DEV] [RFC] switch expression

2020-03-29 Thread G. P. B.
On Sun, 29 Mar 2020 at 23:04, Ilija Tovilo wrote: > > Having two syntaxes for one keyword is not a good idea, > > We're already doing that. What about classes vs anonymous objects? > Functions vs closures? > They're using the same keywords. There's no confusion. > > Ilija > > -- > PHP Internals

Re: [PHP-DEV] [RFC] switch expression

2020-03-29 Thread Ilija Tovilo
> Having two syntaxes for one keyword is not a good idea, We're already doing that. What about classes vs anonymous objects? Functions vs closures? They're using the same keywords. There's no confusion. Ilija -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit:

Re: [PHP-DEV] [RFC] switch expression

2020-03-29 Thread Stanislav Malyshev
Hi! >> I'm still not sure why if we're calling it "switch" > > C# does it. Not to say everything they do is right but it's reassuring > that a big company like Microsoft had the same approach. No it isn't. Having two syntaxes for one keyword is not a good idea, whether Microsoft is doing it or

Re: [PHP-DEV] [RFC] switch expression

2020-03-29 Thread Ilija Tovilo
Hi Stanislav > I'm still not sure why if we're calling it "switch" C# does it. Not to say everything they do is right but it's reassuring that a big company like Microsoft had the same approach. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch

Re: [PHP-DEV] [RFC] switch expression

2020-03-29 Thread Stanislav Malyshev
Hi! > You can take a look at the tests to get a feel for what it’s like: > > https://github.com/php/php-src/pull/5308/files > > Multiple conditions are possible: > > ``` > > return $day switch { > >     1, 7 => false, > >     2, 3, 4, 5, 6 => true, > > }; I'm still not sure why if we're

Re: [PHP-DEV] [RFC] switch expression

2020-03-27 Thread Ilija Tovilo
Hi Gabriel > This doesn't look like it can do fallbacks which are traditional feature of > switch statements, can it? You can take a look at the tests to get a feel for what it’s like: https://github.com/php/php-src/pull/5308/files Multiple conditions are possible: ``` return

Re: [PHP-DEV] [RFC] switch expression

2020-03-27 Thread Gabriel O
This doesn't look like it can do fallbacks which are traditional feature of switch statements, can it? What I mean is this ``` case foo: case bar: return 1; case baz: return 2; ``` If we can't do that, I would suggest to go with different name, rather than reusing "switch".

Re: [PHP-DEV] [RFC] switch expression

2020-03-25 Thread Manuel Canga
Hi, internals, En mié, 25 mar 2020 17:21:29 +0100 Rowan Tommins escribió > On Wed, 25 Mar 2020 at 15:29, Ilija Tovilo wrote: > > > I don't think this would add any significant benefit over: > > > > ```php > > $x = true switch { > > $x !== null && $x < 5 => ... > > }

Re: [PHP-DEV] [RFC] switch expression

2020-03-25 Thread Christoph M. Becker
On 25.03.2020 at 17:06, Dennis Birkholz wrote: > Hello together, > > Am 25.03.20 um 16:46 schrieb Larry Garfield: >> On Wed, Mar 25, 2020, at 10:29 AM, Ilija Tovilo wrote: >>> Thanks for your feedback, Larry! >>> One possible improvement to either version is allowing an expression on

Re: [PHP-DEV] [RFC] switch expression

2020-03-25 Thread Ilija Tovilo
Hi Rowan > The problem with that is that it requires a temporary variable to be > switched on. If I want to switch on, say, a method call, I can write this > for equality: I agree. The iffy part would be recognizing if the case expression should be equated to the switch input or evaluated on

Re: [PHP-DEV] [RFC] switch expression

2020-03-25 Thread Ilija Tovilo
Hi Dennis Thanks for your feedback! > you can fall through to other cases without break You could do the same using the || operator. > what about the default case? I haven't described the default case in my proposal but it is exactly what you'd expect it to be: ```php $var = true switch {

Re: [PHP-DEV] [RFC] switch expression

2020-03-25 Thread Rowan Tommins
On Wed, 25 Mar 2020 at 15:29, Ilija Tovilo wrote: > I don't think this would add any significant benefit over: > > ```php > $x = true switch { > $x !== null && $x < 5 => ... > } > ``` > The problem with that is that it requires a temporary variable to be switched on. If I want to switch

Re: [PHP-DEV] [RFC] switch expression

2020-03-25 Thread Dennis Birkholz
Hello together, Am 25.03.20 um 16:46 schrieb Larry Garfield: > On Wed, Mar 25, 2020, at 10:29 AM, Ilija Tovilo wrote: >> Thanks for your feedback, Larry! >> >>> One possible improvement to either version is allowing an expression on the >>> left side. That is, rather than doing an equality

Re: [PHP-DEV] [RFC] switch expression

2020-03-25 Thread Larry Garfield
On Wed, Mar 25, 2020, at 10:29 AM, Ilija Tovilo wrote: > Thanks for your feedback, Larry! > > > One possible improvement to either version is allowing an expression on the > > left side. That is, rather than doing an equality match, do a boolean > > match. > > This is how Rust does it: > >

Re: [PHP-DEV] [RFC] switch expression

2020-03-25 Thread Ilija Tovilo
Thanks for your feedback, Larry! > One possible improvement to either version is allowing an expression on the > left side. That is, rather than doing an equality match, do a boolean match. This is how Rust does it: ```rust let x = match ... { Some(y) if y < 5 => ... } ``` In other

Re: [PHP-DEV] [RFC] switch expression

2020-03-25 Thread Larry Garfield
On Wed, Mar 25, 2020, at 9:27 AM, Ilija Tovilo wrote: > Hi Michał > > > > I’m sorry, unfortunately I missed your e-mail and RFC. > > Let me know if you’re still working on it and I’ll back off of course. > > > > Regards I like the concept, and it looks like you're both on a similar

Re: [PHP-DEV] [RFC] switch expression

2020-03-25 Thread Ilija Tovilo
Hi Michał I’m sorry, unfortunately I missed your e-mail and RFC. Let me know if you’re still working on it and I’ll back off of course. Regards

Re: [PHP-DEV] [RFC] switch expression

2020-03-25 Thread Michał Brzuchalski
Hi Ilija, śr., 25 mar 2020 o 13:10 Ilija Tovilo napisał(a): > Hi everybody! > > > > A few years ago I suggested adding a new `match` expression to the PHP > language: > > https://externals.io/message/100487 > > > > I arrogantly assumed someone will implement it for me which of course > didn't

[PHP-DEV] [RFC] switch expression

2020-03-25 Thread Ilija Tovilo
Hi everybody! A few years ago I suggested adding a new `match` expression to the PHP language: https://externals.io/message/100487 I arrogantly assumed someone will implement it for me which of course didn't happen. I'd finally like to get my own hands dirty. I have a very rough,