4 ms·
>If I destructure a 3-list to 2 vars, I want it to fail, dammit! Why should that fail any more than: var a = foo[0]; var b = foo[1]; // rest of fo
by ataggart 15y ago
>If I destructure a 3-list to 2 vars, I want it to fail, dammit!
Why should that fail any more than:
var a = foo[0];
var b = foo[1];
// rest of foo unused
Destructing is just another way to access into a list. There's no notion of completeness here, and there's nothing necessarily erroneous with not referring to all the elements. What would you do if you need just the first 5 elements of a 100-element long list?
That said, if they're going to include destructuring, it'd be nice if they also included a way to bind the tail.
- masklinn 15y ago> What would you do if you need just the first 5 elements of a 100-element long list? Depending on the language, either you don't or you use a "everything else" operator to an unused variable: a, b, *_ = list You could even alter the syntax slightly to put a "Don't care" placeholder: a, b, * = list same as above but does not require serializing the [2:] array slice.
- adgar 15y agoRuby unfortunately has non-strict destructuring, but it also has LHS splatting: a, *, c = list And people often use _ as a placeholder when not using splatting: a, _, _, c, d = list
- philjackson 15y agoThey are: var [ a, b, ...rest ] = foo
- ajuc 15y ago> What would you do if you need just the first 5 elements of a 100-element long list? I like Python solution there: list = [1,2,3,4,5,6,7,8,9,10] a,b,c = list[0:3] Also when I do a,b,c = list[0:4] I get "ValueError: too many values to unpack" as it should be - you can always catch the exception and ignore it, if your code really don't care.
- Volpe 15y agoCatch an exception of something that isn't exceptional? isn't that the anti-case for exception handling? I don't get why strict restructuring is a bad thing... as long as it's consistant, it's just a nuance of the language. Am I missing side effect of this? What are potential pit-falls of this pattern?
- ajuc 15y agoI was arguing that non-strict destructuring is bad, so this code IMHO should throw exception: a,b,c=[1,2,3,4] The reason for this is - if you assume array has 3 elements, but it has more, your code will silently ignore the rest. You'll have to write your assertion every time you destructure, to be sure that destructuring don't ignore data. The exceptional case IMHO is when you need to ignore data, and so code for this case should be uglier, not the other way around. You are right that catching exception for regular code path isn't the best way, but at least programmer intention is clear then. You could also do a,b,c = list.slice(0,3); which copies the array, but at least it's clear what it assumes about the array. And it can be easily modified to get last 3 values, or 3 values from the middle of list. So no need for 2 idioms depending on which items you want to get from the array.
- masklinn 15y agoFWIW, Python 3 improves case 1 to: a, b, c, *_ = list better, you can put the slice at at any point of the left hand, so if you want the first and last elements of your list (and want to ignore everything else) you can write: a, *_, b = list