zephir icon indicating copy to clipboard operation
zephir copied to clipboard

Support of Symmetric array destructuring - PHP list()

Open sandrokeil opened this issue 6 years ago • 9 comments

Is it planned to support Symmetric array destructuring?

//prior to php 7.1
list($firstName, $lastName) = ["John", "Doe"];
// php >= 7.1
[$firstName, $lastName] = ["John", "Doe"];

sandrokeil avatar Nov 27 '18 17:11 sandrokeil

I say skip the funky list language construct masquerading as function and just go to the proper destructuring syntax. That is unless the [ and ] tokens are done so inflexible in Zephir that it would require a major refactor.

So:

var firstName, lastName;

let [firstName, lastName] = param;

expands to:

var firstName, lastName;

let firstName = param[0];
let lastName = param[1];

and then later see if it can somehow be improved ever so slightly in performance by hooking into how PHP 7.1+ does it. It probably quite irrelevant though since its so simple.

Anyways I say do it as quickly as possible and then worry about the tiniest optimizations later.

dschissler avatar Nov 28 '18 07:11 dschissler

What do you think about such syntax guys:

array langs = [ "php", "ruby", "perl" ];
var lang1, lang2, lang3;

let lang1, lang2, _     = langs;
let _,     lang2, _     = langs;
let lang1, lang2, lang3 = langs;

sergeyklay avatar Nov 28 '18 07:11 sergeyklay

Is there a problem reusing the bracket tokens?

dschissler avatar Nov 28 '18 08:11 dschissler

Nope. I just thinking about it. This is not implemented yet, but we do not have to consciously over-complicate the end-user syntax, isn't?

sergeyklay avatar Nov 28 '18 08:11 sergeyklay

Well brackets are used for destructuring in PHP and ES6 Javascript. So why model it on some other language or reinvent the wheel?

dschissler avatar Nov 28 '18 08:11 dschissler

In other hand parentheses are used for IF, ELSEIF, CATCH, WHILE, etc in PHP, and ES6 Javascript, but not in Zephir :)

sergeyklay avatar Nov 28 '18 08:11 sergeyklay

Anyway, I'm not debating that we don't need brackets in Zephir at all. It is more a discussion and just my personal opinion

sergeyklay avatar Nov 28 '18 08:11 sergeyklay

If it uses brackets then it would be easier to expand the destructuring to map arrays (someday) without adding another form. I can't think of a syntax for that which wouldn't benefit from being enclosed in brackets. Sure, it could be done but its then starting to look too yamlish in code for my liking.

For example of just one possibility:

let ["LANG_ONE": lang1, "LANG_TWO": lang2]     = langs;

So what do you do without brackets?

let "LANG_ONE": lang1, "LANG_TWO": lang2     = langs;

I've personally been moving away from that last trend of super loose languages.

Another point is that Zephir is very easy to read. The brackets immediately tell first time readers what is going on.

Is there any other language that you are drawing inspiration from for the bracketless syntax? For if there are many other languages that don't use parenthesis and so its easy to know if there are downsides to that. Its not exactly exotic to do if cond {, its just not used in C type languages. Big difference IMO.

dschissler avatar Nov 28 '18 09:11 dschissler

I see, it's not trivial. I'm only asking because I write a PHP to Zephir converter. I will solve it with the curent syntax. So no hurry.

sandrokeil avatar Nov 28 '18 21:11 sandrokeil