From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-3.8 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.1 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 35AA61F453 for ; Fri, 2 Nov 2018 20:29:18 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 61AD1120AA2; Sat, 3 Nov 2018 05:29:15 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id 699DE120945 for ; Sat, 3 Nov 2018 05:29:13 +0900 (JST) Received: by filter0060p3las1.sendgrid.net with SMTP id filter0060p3las1-11202-5BDCB396-D 2018-11-02 20:29:10.261016934 +0000 UTC m=+164245.767882632 Received: from herokuapp.com (ec2-54-242-255-58.compute-1.amazonaws.com [54.242.255.58]) by ismtpd0009p1iad2.sendgrid.net (SG) with ESMTP id q0VpCsrdR96RPf7IfwilVA for ; Fri, 02 Nov 2018 20:29:10.039 +0000 (UTC) Date: Fri, 02 Nov 2018 20:29:10 +0000 (UTC) From: blake.h.l.west@gmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 65067 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15236 X-Redmine-Issue-Author: ignatiusreza X-Redmine-Sender: blakewest X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS74V64FHy2ahZy3pYKhObKiB5a98O8xH2qhVd edYm6G4cpqinZ0l4V4XvjIRzc6S+bdcRffN1LpYLXBLyqphLLUb42vLzOMNh4CDPtDj066JbeU1bpV nVCG4RPq504Y+P1FfVfyak8u/Q7BklsWUkWdQDXMs6G3NzCQyt050AllkQ== X-ML-Name: ruby-core X-Mail-Count: 89682 Subject: [ruby-core:89682] [Ruby trunk Feature#15236] add support for hash shorthand X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #15236 has been updated by blakewest (Blake West). Hi all, I've really wanted a feature like this for a long time. I find myself often using named arguments, which I love for the clarity. But then the callers often have variables of the same name as the parameter name, making it more verbose. eg. `foo(bar: bar, baz: baz)`. If one concern here is that `{a, b, c}` looks like set syntax, then I'd be curious to get opinions on a different syntax I had thought about for a while. The general idea is to extend the `%w` idea for hashes. For example, we could do `%h{foo bar baz}` which would expand into `{foo: foo, bar: bar, baz: baz}`. In both the "%w" and "%h" cases, we're allowing for a more concise version of an often used pattern. I think the downside is using a `%h` syntax feels "separated" from the typical hash syntax. Using a straight `{a,b,c}` is "cleaner" as there are fewer characters, and may be more intuitive to some. But similarly, I think that actually the upside of this syntax is it's more "separated". As Matz points out, the `{a,b,c}` syntax could look like set syntax, and as shevegen points out, it could be confusing for beginners. Neither of these problems would exist using %h, as it would pretty clearly be a "special" syntax, used by people who know what it's doing. Also, using `%h{}` would enable not having to use commas, which could make it even more compact I'll leave with a few more examples of what I'd be suggesting. ~~~ ruby def foo(param1:, param2:) param + param2 end param1 = 7 param2 = 42 foo(%h{param1 param2}) def respond_with(resource, options) meta = extract_meta(resource, options) etc = extract_etc(resource, options) %h{ resource meta etc } end # destructuring could obviously be left for later. %h{data meta etc} = {data: [1,2,3], meta: {mobile: true}, etc: "more info"} ~~~ Thoughts? ---------------------------------------- Feature #15236: add support for hash shorthand https://bugs.ruby-lang.org/issues/15236#change-74722 * Author: ignatiusreza (Ignatius Reza Lesmana) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- PR in github: https://github.com/ruby/ruby/pull/1990 inspired by javascript support for object literal shorthand notation `{ a }`, which will be expanded into `{ a: a }`.. to avoid ambiguity, this shorthand is only supported when hash is defined with `{ }` notation.. in other situation where the brackets is optional, e.g. function call, we still need to write it in full (`m(a : a)` instead of `m(a)`, or `m(a, b, c: c)` instead of `m(a, b, c)`.. -- https://bugs.ruby-lang.org/