From: shevegen@... Date: 2019-06-04T08:15:38+00:00 Subject: [ruby-core:92952] [Ruby trunk Feature#15897] `it` as a default block parameter Issue #15897 has been updated by shevegen (Robert A. Heiler). I was about to write a lengthy reply, but I think it would be too difficult to read for others, so this is a somewhat shorter variant. So just the main gist: (1) I don't quite like the name "it", mostly due to semantics (the name does not tell me much at all), but one advantage is that "it" is short to type. I do not have a good alternative name, though. _ as a name would be even shorter, and avoids some of the semantic-meaning problem, but may have other small issues - see a bit later what I mean here. (2) Even though I do not like the name "it", to me personally, it would be better to see that BOTH @1 @2 and "it" would be added, in the sense that then people could just pick what they prefer. Obviously I see no problem with @1 @2 at all, so I am biased. But that way people could just use whatever they prefer. (There could be another name, of course ... I thought about some way for acessing block/procs data ... such as __BLOCK__ or ProcData or something like that. One problem is that this is all longer to type than e. g. "it" or @1 but perhaps we could use some generic way to access what a proc/block represents anyway, even aside from the proposal itself, similar to ... __LINES__ or __method__ or __FILE__ or so. Just so that we may have a concept for it; although it will probably not be widely used. But I have not really thought about this much. Note that I was wondering about something like this for case/when structures as well, so that we could access them more easily, also from "outside" of methods, but I digress here). IF only "it" alone were to be added, then I would rather prefer that neither @1 @2 nor "it" would be added, and we'd all only use the oldschool way (which is fine, too). But as said, I am biased so I think @1 @2 etc... are perfectly fine. I think this is also a problem I have with the "this is perl" comments in general - to me it is not like perl anywhere. And the old variant such as: foo.each {|a,b,c| just continue to work fine; so to me the change is primarily about adding more flexibilty. This was a problem I have had with the use cases that were mentioned - people would be very eager to point out what they dislike (understandably so), but at the same time would not want to mention use cases that may be useful. So I think in general, it would be better to be more objective in giving examples, even if a certain functionality is disliked. Use cases should ideally be extensive, not just focusing on what the ruby user at hand may dislike the most (since they may tend to focus on that first, which is understandable, and then ignore anything else; I tend to do so myself sometimes). To clarify this - my primary problem with "it" is the name itself, not the functionality that is associated with it. Using _ avoids the name/semantic issue a bit, to some extent, but I think _ has some slight other issues. For example, the _ variable is used quite a lot in ruby code out there, or at the least in some code bases, so I am not sure it would be a good name here. Note that I use _ as "throwaway" variable a lot in my own code. This should be kept in mind, at the least for when ruby users do something similar (I have no idea whether it is common or not, though). Since I like _ a lot, I'd rather see "it" be added than _, because I will most likely not use "it" in my own code ;D , whereas I would still use _ fine, possibly even within blocks, and possibly @1 @2 too, at the least for quick debugging, if it were to stay/remain, which I hope it will. But I am biased. ;) I should also note that while I think @1 @2 are perfectly fine, I also don't have a big problem if it were not to be added permanently, even though I think it is fine if it would, evidently. The oldschool way is the best. Since I see @1 @2 mostly as a convenience feature, though, I can continue to work with ruby just fine. I just don't think that all prior statements in particular in the other thread(s) made a whole lot of sense; and I have no problem if "it" would be added either as well, since I can avoid it, and just use @1 @2 for debugging. ;) I think, realistically, I assume that most ruby users will continue to just use ruby code like it used to be, like: foobar.some_method {|a, b, c, d, _, f| I am quite certain that I will keep on using the above variant, and that neither "it" nor @1 @2 would persist in my own code - but for quick debugging, in particular for longer names, I think @1 @2 is really great. I don't think "it" would be equivalent to this, though; at the least to me, "it" is not the same as e. g. @1 @2 in several ways. But as said, I have no problem at all if both variants would be added. Of course I am not naive - when features are added/offered, people will use it and play around with it; adults are kids after all, just play with different things. ;) I just don't think that the primary focus for dislike should be limited to just some use cases, without considering situations such as e. g. @1 @2 not be used in production code, but just for debugging purposes alone. I don't have a problem with the scenario where we can avoid naming parameters, but to me this is not the primary use case I would like to focus myself - for me the "pp @1; pp @3" variant really is the more important aspect of the suggestion. When you come from this point of view then I think it is easy to understand that "it", aside from the name, is not exactly the same. Last but not least, as mame wrote - I think if you have not yet commented on either @1 @2 (in other issues) and/or "it" (here in this proposal), it may be good to comment on the suggestion/idea itself here. Matz actually asked for feedback before, not only in the other thread but also the old(er) ones predating these. ---------------------------------------- Feature #15897: `it` as a default block parameter https://bugs.ruby-lang.org/issues/15897#change-78329 * Author: mame (Yusuke Endoh) * Status: Open * Priority: Normal * Assignee: matz (Yukihiro Matsumoto) * Target version: ---------------------------------------- How about considering "it" as a keyword for the block parameter only if it is the form of a local varaible reference and if there is no variable named "it"? ``` [1, 2, 3].map { it.to_s } #=> ["1", "2", "3"] ``` If you are familiar with Ruby's parser, this explanation is more useful: NODE_VCALL to "it" is considered as a keyword. Examples: ``` public def it(x = "X") x end [1, 2, 3].map { it.to_s } #=> ["1", "2", "3"] [1, 2, 3].map { self.it } #=> ["X", "X", "X"] # a method call because of a receiver [1, 2, 3].map { it() } #=> ["X", "X", "X"] # a method call because of parentheses [1, 2, 3].map { it "Y" } #=> ["Y", "Y", "Y"] # a method call because of an argument [1, 2, 3].map { it="Y"; it } #=> ["Y", "Y", "Y"] # there is a variable named "it" in this scope it = "Z" [1, 2, 3].map { it.to_s } #=> ["Z", "Z", "Z"] # there is a variable named "it" in this scope ``` Pros: * it is the best word for the feature (according to @matsuda) * it is reasonably compatible; RSpec won't break because their "it" requires an argument Cons: * it actually brings incompatibility in some cases * it is somewhat fragile; "it" may refer a wrong variable * it makes the language semantics dirty Fortunately, it is easy to fix the incompatible programs: just replace `it` with `it()`. (Off topic: it is similar to `super()`.) Just inserting an assignment to a variable "it" may affect another code. This is a bad news, but, IMO, a variable named "it" is not so often used. If this proposal is accepted, I guess people will gradually avoid the variable name "it" (like "p"). The dirtiness is the most serious problem for me. Thus, I don't like my own proposal so much, honestly. But it would be much better than Perlish `@1`. (Note: I don't propose the removal of `@1` in this ticket. It is another topic.) In any way, I'd like to hear your opinions. An experimental patch is attached. The idea is inspired by @jeremyevans0's [proposal of `@`](https://bugs.ruby-lang.org/issues/15723#note-98). P.S. It would be easy to use `_` instead of `it`. I'm unsure which is preferable. ---Files-------------------------------- its.patch (4.92 KB) -- https://bugs.ruby-lang.org/ Unsubscribe: