3 ms·
that's awful syntax IMHO. like php's list/map but with several levels. those are just asking for bugs that are hard to spot even on code reviews.
by gcb0 11y ago
that's awful syntax IMHO.
like php's list/map but with several levels.
those are just asking for bugs that are hard to spot even on code reviews.
- implicit 11y agoOther languages have implemented this idea for decades now. It's a great feature. The main thing that bothers me is that the default behaviour is too permissive. let a = [1,2,3,4,5]; let [b] = a; // ok. b receives 1 The equivalent Python will fail because there are "too many values to unpack."
- jeswin 11y agoI prefer the JS version. If you're a dynamic language, be unapologetically so. /* I can write code independent of getCheapestSellers() impl */ var [bestPrice, secondBest] = getCheapestSellers("product_name");
- DasIch 11y agoThat has nothing to do with being dynamic or not. Besides your example translates quite nicely to Python as well: [bestPrice, secondBest, _*] = getCheapestSellers('product_name')
- jeswin 11y agoMy question is, why? If one wanted this type of guarantee consistently, they might as well use a statically typed language. Add: To clarify, the error shows up at runtime in Python. So you're going to need tests anyway. Just as in JS. Btw (unrelated), you can do the same thing in JS as well [a, b, ...rest] = [1, 2, 3, 4, 5]
- deleted 11y ago[deleted]
- tracker1 11y agoYay, you now have a variable declaration you aren't even going to use... Assuming "_" in this case is a variable, and not some magic keyword in the language, which is probably more stupid than just ignoring the rest instead of throwing an error. (forgive my Python ignorance)
- DasIch 11y ago_ is just a name and using it for values you are not going to use is after assignment is a convention in Python, Haskell and probably other languages that support pattern matching or destructuring. Keep in mind this also comes up in cases like: foo, _, bar = something() In general you can't avoid doing something like it, why "optimize" for this one special case?
- tracker1 11y agoWhy throw an error for something that isn't needed? This is always something I like about JS... it doesn't throw an error in many situations, where the most likely case would be to suppress it anyway, or when there's no point in throwing one...
- KMag 11y ago> Why throw an error for something that isn't needed? Because it appears the programmer's assumptions have been violated at this point. There's a syntactical way for destructuring assignment of a variable-length list, and it wasn't used. Maybe the programmer was being lazy, or maybe the programmer's assumptions were violated. How do you feel about [a, b, c, d] = [1, 2] ? Should it throw? > it doesn't throw an error in many situations, where the most likely case would be to suppress it anyway, or when there's no point in throwing one... PHP rightly gets lots of flak for attempting to lumber on after the programmer probably made a mistake. JavaScript isn't as bad, but it does hide a fair number of programmer mistakes. For small web apps where failure or incorrect data isn't a big deal, maybe hiding small bugs is the right thing. For large applications, hiding likely programmer laziness or minor bugs is not a good feature.
- implicit 11y agoIt's really valuable for mistakes to result in clear errors rather than confusing but "valid" programs. Even worse: let [a, b, c, d] = [2]; a receives 2. b, c, and d receive undefined.
- nightpool 11y agoWait, side note, but when you think of functional programming your first point of reference is PHP? More then Javascript (which I'm assuming you're probably familiar with, to have such a strong opinion about its syntax)? Am I interpreting this wrong?
- gcb0 11y agoit's not that far away in the sense that one started as functional language and was made c-like, and the other started c-like and was made functional :)