-
Notifications
You must be signed in to change notification settings - Fork 62
Give warning for unused destructured variables #4531
Comments
[@lucaswerkmeister] But what if I only need |
[@gavinking] That would be like the most annoying feature ever! Can I close this? |
Related note: it might signal an overuse of value [x, y, _] = [ 1, 2, 3 ];
value [a, __, c] = [ 1, 2, 3 ]; // can't use '_' again! but would prefer something like: value [x, y,] = [ 1, 2, 3 ];
value [a, ,c] = [ 1, 2, 3 ]; |
[@gavinking] Yeah @jvasileff same thing happens to me. I have toyed with the idea of adding a |
[@gdejohn] I'd like it if
|
[@gavinking]
Not sure if that helps ;-) |
[@lucaswerkmeister] I don’t like the look of |
[@quintesse] Together with the option to ignore specific items (not just a |
[@quintesse] So let's rename this issue to "allow tuple items to be ignored when destructuring" and have the warning as a consequence of implementing that. |
[@jvasileff] 👍 on the special meaning for |
[@lucaswerkmeister] Is the special meaning only in destructuring, or in general? (Could also be useful in, e. g., anonymous function params.) |
[@jvasileff] I would say in general. |
[@lucaswerkmeister] Me too, seems weird to have special variable names only in a few contexts. OTOH that’s quite a big language change… |
[@jvasileff] Initially, I imagine it would work to only support |
[@FroMage] I personally don't see why value x = 2; Would result in an "unused" warning, but value [x] = [2]; Would not. It sounds so arbitrary and illogical. I'd much rather have a special character for ignored bits, but I also think that's a feature which can wait for 1.3. |
[@Zambonifofex] I myself don't like how |
[@gavinking] I would also prefer something other than |
[@jvasileff] I really dislike the underscore character. Sort of wish it were never invented. But in this case, I think it is the most straight forward and intuitive option, immediately obvious to new readers, and void of visual clutter (the reason it's intuitive.) |
[@gdejohn] Underscore is used for this by many other languages: Scala, Haskell, F#, Erlang, ML, Prolog. I'm not aware of anyone who does it differently. |
[@gavinking] I guess I could just give |
[@pthariensflame] If you really wanted a different character than |
[@luolong] Well, seeing how using Also, I've found myself using exactly this convention in many cases where I want to ignore the variable. Usually I've want to ignore just one argument though, so I never really stumbled upon a situation where I would feel that I think easiest fix would probably be to treat identifiers consisting only of underscore characters specially in that they would not produce a warning of unused variables. We could revisit giving single underscore a special status some time later? |
[@tombentley] For a long time I didn't know what If I was writing this sort of code, I'd just use the name |
[@FroMage] Or: value [first,,third,] = [1,2,3,4,5,6]; That is, skip the second and last elements. |
[@luolong] Well, to be honest, Still I would vote for TC treating |
Sure, but that's just ignoring the whole discussion we're having here ;) We have warnings for unused variables probably because people find them useful as warnings that you've forgotten something. For the case of single values it's easy if you want to call a method and not do anything with its return value so you won't get any warnings: you just don't assign it to a variable (duh). But you can't do that with tuples. So we've decided to just don't give warnings, which is the easy way out, but now we've lost our warning that tells as something might be wrong. To me personally I don't see anything wrong with just leaving a gap: Edit: What @FroMage says in one line ;) |
[@luolong] @quintesse, this is example looks somewhat nice, but it also opens up a chance for unintended errors. What if the extra comma is a typo? |
[@luolong] What I want to say is, that we need some way for a developer to show intention to ignore the item between commas... |
Seriously? It seems to me that that is just as likely to be a typo as it is intentional (especially the trailing comma one). I think that would be worse than the current situation. |
[@lucaswerkmeister] I really don’t like the |
[@quintesse] I'd suggest this:
|
[@luolong] As for discoverability, when we do not overload underscore with too many meanings, it is really very intuitive. As the spec says, underscore is a perfectly valid identifier. There is nothing new to learn. |
[@quintesse] @lucaswerkmeister you might not "like" it, estethically speaking, but I think it's the only option that won't confuse people (not without taking away the ability to use |
[@quintesse] I mean when people start speaking about special meanings for |
[@luolong] The trouble with underscores in Scala for example is that it has too many meanings. In Ceylon, an identifier consisting of just underscores is just another variable. Same as everything else. As a convenient convention though, we can by default suppress any "unused" warnings for identifiers that consist of only underscores (or even more strictly, only a single underscore) |
And do things like:
? yew. |
[@FroMage] Yeah, I find The hole syntax does not have these questions. BUT I agree with @tombentley it may be written by mistake by a programmer who did not intend to write that double comma. Except that if we do strict number of items matching, for it to be a mistake you'd need to have two mistakes, because that would just not pass if you had the right number of variables and a comma mistake: value [a,,b] = [1,2]; So you'd need to have a comma typo and a missing variable name or value for it to be an unnoticed mistake. As for @quintesse's suggestion of requiring a |
[@gavinking] The typechecker would pick up any typo related to too many commas. It knows how many "slots" there are in a tuple. OTOH that approach doesn't work well for entries. |
[@FroMage] Relevant Perl manual:
|
[@FroMage]
What's the special case there? |
Huh? There's no special case. We support for destructuring for entries wherever you can destructure a tuple. I mean, I suppose this is sort of OK:
But I definitely don't love it. |
[@FroMage] I don't think we have the same problem with entries, since they can only have two parts (so ignoring one is less common), and you can always use |
[@quintesse] @FroMage you really want to use Perl as a source of inspiration??? 😉 |
[@FroMage] Well, the language as a whole has many good features, like C++ or Scala. It's the sum of the features that doesn't make them great languages, not their parts. |
[@luolong]
Well, if you throw away half of the items in a tuple, why are you using destructuring then? Actually, the following would not look too ugly either: value [one, two, ignore, ignore, ignore, six] = [1, 2, 3, 4, 5, 6]; |
[@lucaswerkmeister] I don’t like how |
[@luolong] True. I do prefer underscore for the same reason. But this discussion is getting kind of religious ... |
[@lucaswerkmeister] True. I think if we’re not going to make any radical changes before 1.2 (like |
[@ePaul] Maybe as a compromise, warn when all of the variables are unused? |
Faced this in my code today and couldn't remember if the question had been settled. Apparently not. So how about value [one, two, void, void, void, six] = [1, 2, 3, 4, 5, 6]; I like the fact that value [one, two, void] = [1, 2, 3, 4, 5, 6]; |
a question. Is currently this possible: value [one, two, *_, six] = [1, 2, 3, 4, 5, 6]; I think this would solve most of the "ugliness" of most patterns of ignoring some of the tuple elements. |
[@quintesse] The following code:
Does not result in warnings for unused variables
x
,y
andz
as expected.[Migrated from ceylon/ceylon-spec#1425]
The text was updated successfully, but these errors were encountered: