zephir-lang / zephir

Zephir is a compiled high-level language aimed to ease the creation of C-extensions for PHP
https://zephir-lang.com
MIT License
3.29k stars 466 forks source link

Support of Symmetric array destructuring - PHP list() #1765

Open sandrokeil opened 5 years ago

sandrokeil commented 5 years ago

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"];
dschissler commented 5 years ago

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.

sergeyklay commented 5 years ago

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;
dschissler commented 5 years ago

Is there a problem reusing the bracket tokens?

sergeyklay commented 5 years ago

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?

dschissler commented 5 years ago

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

sergeyklay commented 5 years ago

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

sergeyklay commented 5 years ago

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

dschissler commented 5 years ago

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.

sandrokeil commented 5 years ago

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.