ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: keystonelemur@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:100635] [Ruby master Feature#17292] Hash Shorthand / Punning
Date: Thu, 29 Oct 2020 05:07:19 +0000 (UTC)	[thread overview]
Message-ID: <redmine.issue-17292.20201029050718.7576@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17292.20201029050718.7576@ruby-lang.org

Issue #17292 has been reported by baweaver (Brandon Weaver).

----------------------------------------
Feature #17292: Hash Shorthand / Punning
https://bugs.ruby-lang.org/issues/17292

* Author: baweaver (Brandon Weaver)
* Status: Open
* Priority: Normal
----------------------------------------
### Set Literal vs Javascript Object Punning

There was a proposal for a Set literal here: https://bugs.ruby-lang.org/issues/16989

```ruby
set = { 1, 2, 3 }
```

...but it was brought up that this is similar to the Javascript Object punning, or Object shorthand syntax:

```js
const a = 1, b = 2, c = 3;

const punnedObject = { a, b, c }
// => { a: 1, b: 2, c: 3 }
```

**Proposition**: I believe we should use brackets (`{}`) for a shorthand Hash syntax similar to Javascript.

### Hash Punning

My first proposal in this feature request is Hash punning, or Hash shorthand:

```ruby
a = 1
b = 2
c = 3

{ a:, b:, c: }
# => { a: 1, b: 2, c: 3 }
```

This syntax avoids the ambiguous syntax of empty block (`{}`) versus empty set (`{}`), and with the presence of Symbols it introduces a distinct syntax that would be easier to parse against.

One potential issue would be mixed syntax:

```ruby
{ a:, b: 2 }
# => { a: 1, b: 2 }
```

### Method Punning

This syntax can also be used for keyword argument and method call punning:

```ruby
def method_name(a:, b:, c:)
  a + b + c
end

a = 1
b = 2
c = 3

method_name(a:, b:, c:)
# => 6
```

I believe this existing syntax for required keywords gives credence to the idea of introducing punning to Ruby, as it's very similar to existing syntax, and therefor feels "Ruby-like".

### Pattern Matching

This syntax is also already present and used in pattern matching, making it already part of the language:

```ruby
case { x: 1, y: 2 }
in { x:, y: }
  { x:, y: y + 1} # new
else
  # ...
end
```

I believe this further justifies the case for punning syntax.



-- 
https://bugs.ruby-lang.org/

       reply	other threads:[~2020-10-29  5:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-29  5:07 keystonelemur [this message]
2020-10-29  6:26 ` [ruby-core:100638] [Ruby master Feature#17292] Hash Shorthand / Punning keystonelemur

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.issue-17292.20201029050718.7576@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).