ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:89468] [Ruby trunk Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
@ 2018-10-19 10:33 ` lyoneil.de.sire
  2018-10-19 14:13 ` [ruby-core:89475] " shevegen
                   ` (16 subsequent siblings)
  17 siblings, 0 replies; 18+ messages in thread
From: lyoneil.de.sire @ 2018-10-19 10:33 UTC (permalink / raw)
  To: ruby-core

Issue #15236 has been reported by ignatiusreza (Ignatius Reza Lesmana).

----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236

* 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:89475] [Ruby trunk Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
  2018-10-19 10:33 ` [ruby-core:89468] [Ruby trunk Feature#15236] add support for hash shorthand lyoneil.de.sire
@ 2018-10-19 14:13 ` shevegen
  2018-10-19 14:13 ` [ruby-core:89476] " shevegen
                   ` (15 subsequent siblings)
  17 siblings, 0 replies; 18+ messages in thread
From: shevegen @ 2018-10-19 14:13 UTC (permalink / raw)
  To: ruby-core

Issue #15236 has been updated by shevegen (Robert A. Heiler).


Hmm. It's hard for me to say whether I am in favour of this suggestion or
whether I am not.

I think this link may help a bit in regards to JavaScript, even though
JavaScript is not Ruby; neither is the syntax:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer

Old JavaScript variant:

    var o = {};
    var o = {a: 'foo', b: 42, c: {}};

versus New JavaScript variant:

    var a = 'foo', b = 42, c = {};
    var o = {a, b, c};

I understand the second part being more convenient and more concise. I am not
really sure whether it makes sense for ruby to adopt this, though.

The part where "omission means something more", is sometimes confusing.

I myself got used to be able to omit {} in a method definition, such as
your example:

    m(a, b, c: c)

which I think would be this:

    m(a, b, { c: c })

I also use the somewhat new Hash syntax in ruby a lot, like:

    foo: :bar

versus the old variant (but still the "real" variant)

    :foo => :bar

I am not entirely sure about the new omission-meaning-infinity
in ranges ( 1 .. ) either, or { a } meaning { a: a } like in the
proposal here, where a is a variable that must exist already,
if { a } is to work. This also reminds me a bit about the 
shortcut suggestion for initializing instance variables within
the method-argument, rather than the body of the method at 
hand (usually "def initialize").

I don't really have a definite pro or con view but I think it
should be thought through for some time either way. While experienced
ruby developers have it easy learning new syntax parts, newcomers may
have to gradually learn more and  more syntax parts, which may not be
ideal for them, even if the new syntax may be shorter. Or where the
syntax allows us to do more with {}, rather than with Hash.new - that
should also be considered to evaluate all trade-offs and
advantages/disadvantages.

If you would like to, you could add your suggestion to any upcoming
developer meeting where you could get some opinions from the ruby core
team and of course matz (which would be at
https://bugs.ruby-lang.org/issues/15229 for the next one in November
2018; or perhaps for a later one in January 2019 since I assume 
most energy of the team may go into the upcoming x-mas release of
ruby :) ).

----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236#change-74516

* 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:89476] [Ruby trunk Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
  2018-10-19 10:33 ` [ruby-core:89468] [Ruby trunk Feature#15236] add support for hash shorthand lyoneil.de.sire
  2018-10-19 14:13 ` [ruby-core:89475] " shevegen
@ 2018-10-19 14:13 ` shevegen
  2018-10-19 14:16 ` [ruby-core:89477] " shevegen
                   ` (14 subsequent siblings)
  17 siblings, 0 replies; 18+ messages in thread
From: shevegen @ 2018-10-19 14:13 UTC (permalink / raw)
  To: ruby-core

Issue #15236 has been updated by shevegen (Robert A. Heiler).


Ah, Nobu found it and was faster. :)

So it was indeed a duplicate.

----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236#change-74517

* 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:89477] [Ruby trunk Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2018-10-19 14:13 ` [ruby-core:89476] " shevegen
@ 2018-10-19 14:16 ` shevegen
  2018-10-19 16:23 ` [ruby-core:89478] " edchick
                   ` (13 subsequent siblings)
  17 siblings, 0 replies; 18+ messages in thread
From: shevegen @ 2018-10-19 14:16 UTC (permalink / raw)
  To: ruby-core

Issue #15236 has been updated by shevegen (Robert A. Heiler).


Matz wrote in the other thread the following:

"I am not positive about this syntax mostly because it appears to be set syntax, or old style hash in 1.8.
Once ES6 syntax become more popular, there will be chance for this change in the future.

Matz."

So I guess it could be discussed at another developer meeting in the future.

----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236#change-74518

* 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:89478] [Ruby trunk Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2018-10-19 14:16 ` [ruby-core:89477] " shevegen
@ 2018-10-19 16:23 ` edchick
  2018-10-20  2:23 ` [ruby-core:89488] " matz
                   ` (12 subsequent siblings)
  17 siblings, 0 replies; 18+ messages in thread
From: edchick @ 2018-10-19 16:23 UTC (permalink / raw)
  To: ruby-core

Issue #15236 has been updated by Ksec (Ed Chick).


shevegen (Robert A. Heiler) wrote:
> Matz wrote in the other thread the following:
> 
> "I am not positive about this syntax mostly because it appears to be set syntax, or old style hash in 1.8.
> Once ES6 syntax become more popular, there will be chance for this change in the future.
> 
> Matz."
> 
> So I guess it could be discussed at another developer meeting in the future.

 But that comment was made three years ago. Time to do another review? 

----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236#change-74520

* 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:89488] [Ruby trunk Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2018-10-19 16:23 ` [ruby-core:89478] " edchick
@ 2018-10-20  2:23 ` matz
  2018-10-22  2:07 ` [ruby-core:89503] " lyoneil.de.sire
                   ` (11 subsequent siblings)
  17 siblings, 0 replies; 18+ messages in thread
From: matz @ 2018-10-20  2:23 UTC (permalink / raw)
  To: ruby-core

Issue #15236 has been updated by matz (Yukihiro Matsumoto).


As a conservative old timer who does not use JavaScript at all, I still feel negative. It seems to work best with destructuring (left-hand side of assignments) which is nearly impossible in current Ruby syntax.

But at the same time, I admit many of Ruby users use Rails and JavaScript, so I am open to hearing your opinion.

Matz.


----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236#change-74529

* 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:89503] [Ruby trunk Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2018-10-20  2:23 ` [ruby-core:89488] " matz
@ 2018-10-22  2:07 ` lyoneil.de.sire
  2018-10-22  8:54 ` [ruby-core:89507] " janfri26
                   ` (10 subsequent siblings)
  17 siblings, 0 replies; 18+ messages in thread
From: lyoneil.de.sire @ 2018-10-22  2:07 UTC (permalink / raw)
  To: ruby-core

Issue #15236 has been updated by ignatiusreza (Ignatius Reza Lesmana).


Hi guys,

Thanks for the discussions! Sorry I didn't noticed that it was proposed (multiple times) before.. I tried to search, but couldn't find a hit..

The ES6 syntax that this gets inspired from is strongly becoming the standard now, partly thanks to it being enabled by default in https://www.npmjs.com/package/eslint-config-airbnb-base

I found a strong the desire for this syntax especially when working on API server alongside JavaScript heavy front end, where one would need to work a lot with building hashes to be transformed into JSON string.. hence, the primary use case where i'm interested in is in building hashes as return value of method call, e.g.

~~~ ruby
def respond_with(resource, options)
  meta = extract_meta(resource, options)
  etc = extract_etc(resource, options)

  { resource, meta, etc }
end
~~~

having

~~~ ruby
{ resource, meta, etc }
~~~

is much more concise and cleaner compared to

~~~ ruby
{ resource: resource, meta: meta, etc: etc }
~~~

within this context, `{ }` is already non-optional, and the new syntax increase readability and save a lot of typing..

To address the concern in https://bugs.ruby-lang.org/issues/11105 .. I think, I agree that this shorthand syntax should only be allowed for `a`, but not for `@a`, `@@a`, or `$a` to avoid ambiguity in what key should be generated for everything else other than `a`..

----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236#change-74556

* 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:89507] [Ruby trunk Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2018-10-22  2:07 ` [ruby-core:89503] " lyoneil.de.sire
@ 2018-10-22  8:54 ` janfri26
  2018-10-30  0:47 ` [ruby-core:89623] " lyoneil.de.sire
                   ` (9 subsequent siblings)
  17 siblings, 0 replies; 18+ messages in thread
From: janfri26 @ 2018-10-22  8:54 UTC (permalink / raw)
  To: ruby-core

Issue #15236 has been updated by janfri (Jan Friedrich).


matz (Yukihiro Matsumoto) wrote:
> As a conservative old timer who does not use JavaScript at all, I still feel negative. It seems to work best with destructuring (left-hand side of assignments) which is nearly impossible in current Ruby syntax.
> 
> But at the same time, I admit many of Ruby users use Rails and JavaScript, so I am open to hearing your opinion.
> 
> Matz.

I'm also don't use JavaScript. I think the idea using destructuring is much more Rubyish than the ES6 syntax.


----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236#change-74560

* 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:89623] [Ruby trunk Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
                   ` (7 preceding siblings ...)
  2018-10-22  8:54 ` [ruby-core:89507] " janfri26
@ 2018-10-30  0:47 ` lyoneil.de.sire
  2018-11-02 20:29 ` [ruby-core:89682] " blake.h.l.west
                   ` (8 subsequent siblings)
  17 siblings, 0 replies; 18+ messages in thread
From: lyoneil.de.sire @ 2018-10-30  0:47 UTC (permalink / raw)
  To: ruby-core

Issue #15236 has been updated by ignatiusreza (Ignatius Reza Lesmana).


janfri (Jan Friedrich) wrote:
> I think the use of destructuring for this is much more Rubyish than the ES6 syntax.

Agreed that destructuring hash is very much ruby, since destructuring array is already supported.. but, I think supporting that does not mean that we can't support this one too.. in fact, I think supporting both would be best, since I would expect that if I can do one, I should also be able to do the other..



----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236#change-74661

* 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:89682] [Ruby trunk Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
                   ` (8 preceding siblings ...)
  2018-10-30  0:47 ` [ruby-core:89623] " lyoneil.de.sire
@ 2018-11-02 20:29 ` blake.h.l.west
  2018-11-02 20:47 ` [ruby-core:89683] " manga.osyo
                   ` (7 subsequent siblings)
  17 siblings, 0 replies; 18+ messages in thread
From: blake.h.l.west @ 2018-11-02 20:29 UTC (permalink / raw)
  To: 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:89683] [Ruby trunk Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
                   ` (9 preceding siblings ...)
  2018-11-02 20:29 ` [ruby-core:89682] " blake.h.l.west
@ 2018-11-02 20:47 ` manga.osyo
  2018-11-02 20:55 ` [ruby-core:89684] " manga.osyo
                   ` (6 subsequent siblings)
  17 siblings, 0 replies; 18+ messages in thread
From: manga.osyo @ 2018-11-02 20:47 UTC (permalink / raw)
  To: ruby-core

Issue #15236 has been updated by osyo (manga osyo).


hi,  blakewest.
I proposaled it.
https://bugs.ruby-lang.org/issues/14973
I need to consider an implementation that does not use `#eval`.

----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236#change-74723

* 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:89684] [Ruby trunk Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
                   ` (10 preceding siblings ...)
  2018-11-02 20:47 ` [ruby-core:89683] " manga.osyo
@ 2018-11-02 20:55 ` manga.osyo
  2018-11-06  1:21 ` [ruby-core:89716] " lyoneil.de.sire
                   ` (5 subsequent siblings)
  17 siblings, 0 replies; 18+ messages in thread
From: manga.osyo @ 2018-11-02 20:55 UTC (permalink / raw)
  To: ruby-core

Issue #15236 has been updated by osyo (manga osyo).


I would like to use Hash shorthand in the following cases.

```
describe User do
	let(:id) { ... }
	let(:name) { ... }
	let(:age)  { ... }
	let(:registered_at) { ... }

	# I want to hash shorthand!
	let(:user) { User.new(id: id, age: age, name: name, registered_at: registered_at) }
	context '`name` is nil' do
		let(:name) { nil }
		it { ... }
	end
	context '`age` is nil' do
		let(:age) { nil }
		it { ... }
	end
end
```


----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236#change-74724

* 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:89716] [Ruby trunk Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
                   ` (11 preceding siblings ...)
  2018-11-02 20:55 ` [ruby-core:89684] " manga.osyo
@ 2018-11-06  1:21 ` lyoneil.de.sire
  2019-04-29 17:56 ` [ruby-core:92481] " tleish
                   ` (4 subsequent siblings)
  17 siblings, 0 replies; 18+ messages in thread
From: lyoneil.de.sire @ 2018-11-06  1:21 UTC (permalink / raw)
  To: ruby-core

Issue #15236 has been updated by ignatiusreza (Ignatius Reza Lesmana).


blakewest (Blake West) wrote:
> 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?

osyo (manga osyo) wrote:
> hi,  blakewest.
> I proposaled it.
> https://bugs.ruby-lang.org/issues/14973
> I need to consider an implementation that does not use `#eval`.

I like this :+1:

`{ a, b, c }` looks a bit cleaner to me, probably because of my familiarity with es6 syntax, so it require less context switching.. but if it is considered confusing to some, `%h{a b c}` also works for me..

----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236#change-74761

* 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:92481] [Ruby trunk Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
                   ` (12 preceding siblings ...)
  2018-11-06  1:21 ` [ruby-core:89716] " lyoneil.de.sire
@ 2019-04-29 17:56 ` tleish
  2019-06-09  3:11 ` [ruby-core:93030] " lyoneil.de.sire
                   ` (3 subsequent siblings)
  17 siblings, 0 replies; 18+ messages in thread
From: tleish @ 2019-04-29 17:56 UTC (permalink / raw)
  To: ruby-core

Issue #15236 has been updated by tleish (Tony Fenleish).


+1

I also vote for this option for both hashes and named params.  After using Es6 and going back to Ruby, the ruby way felt clunky to me.

There are multiple reasons why this would be advantageous.

## Readability: 

I like the named params that was added to Ruby.  It allows flexibility in the order of the params.  but often struggle in convincing myself to use them because of readability.   Code examples often show the following:

```ruby
my_hash = {name: 'joe', age: 8}

# or

my_method(name: 'Joe', age: 8)
```

But in the real world, the values are usually stored in other variables and 95% of the time the code looks like this:

```ruby
my_hash = {name: name, age: age}

# or

my_method(name: name, age: age)
```

The above code does not ready well with it's redundant words.  With the change suggested, it reads so much better:

```ruby
my_hash = {name, age}

# or

my_method(name, age)
```

If the variable is different than the param name, then it easily communicates that:

```ruby
my_hash = {name, age: calculate_age}

# or

my_method(name, age: calculate_age)
```

## Refactoring

If a method is implemented without keyword arguments:

```ruby
def my_method(name, age); end

my_method(name, age)
```

And then later it is decided to refactor using ruby keywords

```ruby
def my_method(name:, age:, address: nil); end
```

This of course is a breaking change.  All locations which use this method now need to be updated to use the new convention.

```ruby
my_method(name: name, age: age)
```

However, for instances where the method was was implemented using variables of the same as the parameters (which often they are), then the change is non-breaking.

```ruby
my_method(name, age) # no change required
```



----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236#change-77827

* 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:93030] [Ruby trunk Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
                   ` (13 preceding siblings ...)
  2019-04-29 17:56 ` [ruby-core:92481] " tleish
@ 2019-06-09  3:11 ` lyoneil.de.sire
  2019-06-09  4:22 ` [ruby-core:93032] " lyoneil.de.sire
                   ` (2 subsequent siblings)
  17 siblings, 0 replies; 18+ messages in thread
From: lyoneil.de.sire @ 2019-06-09  3:11 UTC (permalink / raw)
  To: ruby-core

Issue #15236 has been updated by ignatiusreza (Ignatius Reza Lesmana).


I was reading the presentation slide for the experimental feature for syntax matching.. in which, there's support for "shorthand" matching with hash, in the form of `{ a: }`.. or, following the examples presented above:

```
def foo(param1:, param2:)
  param + param2
end

param1 = 7
param2 = 42

foo(param1:, param2:)

def respond_with(resource, options)
  meta = extract_meta(resource, options)
  etc = extract_etc(resource, options)

  { resource:, meta:, etc: }
end

# destructuring
{data:, meta:, etc:} = {data: [1,2,3], meta: {mobile: true}, etc: "more info"}
```

I think, this answers the concern mentioned above regarding `{ a }` being confused with a Set instead of Hash.. wydt?

ref: https://speakerdeck.com/k_tsj/pattern-matching-new-feature-in-ruby-2-dot-7?slide=34

----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236#change-78404

* 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:93032] [Ruby trunk Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
                   ` (14 preceding siblings ...)
  2019-06-09  3:11 ` [ruby-core:93030] " lyoneil.de.sire
@ 2019-06-09  4:22 ` lyoneil.de.sire
  2019-07-29  8:01 ` [ruby-core:93983] [Ruby master " ko1
  2019-08-11 15:12 ` [ruby-core:94276] " FreeKMan
  17 siblings, 0 replies; 18+ messages in thread
From: lyoneil.de.sire @ 2019-06-09  4:22 UTC (permalink / raw)
  To: ruby-core

Issue #15236 has been updated by ignatiusreza (Ignatius Reza Lesmana).


PR for alternative syntax `{ a: }`: https://github.com/ruby/ruby/pull/2231

----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236#change-78417

* 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:93983] [Ruby master Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
                   ` (15 preceding siblings ...)
  2019-06-09  4:22 ` [ruby-core:93032] " lyoneil.de.sire
@ 2019-07-29  8:01 ` ko1
  2019-08-11 15:12 ` [ruby-core:94276] " FreeKMan
  17 siblings, 0 replies; 18+ messages in thread
From: ko1 @ 2019-07-29  8:01 UTC (permalink / raw)
  To: ruby-core

Issue #15236 has been updated by ko1 (Koichi Sasada).

Status changed from Open to Rejected

Please reopen (re-create) new proposal which can persuade Matz.


----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236#change-80177

* Author: ignatiusreza (Ignatius Reza Lesmana)
* Status: Rejected
* 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:94276] [Ruby master Feature#15236] add support for hash shorthand
       [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
                   ` (16 preceding siblings ...)
  2019-07-29  8:01 ` [ruby-core:93983] [Ruby master " ko1
@ 2019-08-11 15:12 ` FreeKMan
  17 siblings, 0 replies; 18+ messages in thread
From: FreeKMan @ 2019-08-11 15:12 UTC (permalink / raw)
  To: ruby-core

Issue #15236 has been updated by D1mon (Dim F).


Well, if the syntax is misleading or intersects with an existing design. Make another syntax to work, why cancel (reject) a very cool thing which will make the code smaller (write less) and it will be nice to read the code. ? Please do not reject the request, redo it and add it to the "ruby core". People have been asking for this opportunity for several years now, please do not refuse them. Thank.

----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236#change-80599

* Author: ignatiusreza (Ignatius Reza Lesmana)
* Status: Rejected
* 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/

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2019-08-11 15:12 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-15236.20181019103301@ruby-lang.org>
2018-10-19 10:33 ` [ruby-core:89468] [Ruby trunk Feature#15236] add support for hash shorthand lyoneil.de.sire
2018-10-19 14:13 ` [ruby-core:89475] " shevegen
2018-10-19 14:13 ` [ruby-core:89476] " shevegen
2018-10-19 14:16 ` [ruby-core:89477] " shevegen
2018-10-19 16:23 ` [ruby-core:89478] " edchick
2018-10-20  2:23 ` [ruby-core:89488] " matz
2018-10-22  2:07 ` [ruby-core:89503] " lyoneil.de.sire
2018-10-22  8:54 ` [ruby-core:89507] " janfri26
2018-10-30  0:47 ` [ruby-core:89623] " lyoneil.de.sire
2018-11-02 20:29 ` [ruby-core:89682] " blake.h.l.west
2018-11-02 20:47 ` [ruby-core:89683] " manga.osyo
2018-11-02 20:55 ` [ruby-core:89684] " manga.osyo
2018-11-06  1:21 ` [ruby-core:89716] " lyoneil.de.sire
2019-04-29 17:56 ` [ruby-core:92481] " tleish
2019-06-09  3:11 ` [ruby-core:93030] " lyoneil.de.sire
2019-06-09  4:22 ` [ruby-core:93032] " lyoneil.de.sire
2019-07-29  8:01 ` [ruby-core:93983] [Ruby master " ko1
2019-08-11 15:12 ` [ruby-core:94276] " FreeKMan

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).