Project

General

Profile

Feature #14579

Hash value omission

Added by shugo (Shugo Maeda) over 1 year ago. Updated over 1 year ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:85950]

Description

How about to allow value omission in Hash literals:

x = 1
y = 2
h = {x:, y:}
p h #=> {:x=>1, :y=>2}

And in keyword arguments:

def login(username: ENV["USER"], password:)
  p(username:, password:)
end

login(password: "xxx") #=> {:username=>"shugo", :password=>"xxx"}

Files

hash_value_omission.diff (619 Bytes) hash_value_omission.diff shugo (Shugo Maeda), 03/06/2018 01:51 PM

Related issues

Related to Ruby trunk - Feature #11105: ES6-like hash literalsRejectedActions

History

#1

Updated by shugo (Shugo Maeda) over 1 year ago

Updated by Eregon (Benoit Daloze) over 1 year ago

I find this syntax very confusing.

The two occurrences of password: above means two very different things (required kwarg and auto-hash creation).

For

x = 1
y = 2
h = {x:, y:}

it looks to me like the values are missing.
I'd prefer a syntax which is different than "key syntax without value", and refers to the variable name used for the value more clearly, like:

x = 1
y = 2
h = {x, y}

That would also work for the second case like so:

def login(username: ENV["USER"], password:)
  p({username, password})
end

Updated by Eregon (Benoit Daloze) over 1 year ago

Should this also work for non-Symbols keys like:

x = 1
y = 2
h = { "x" => , "y" => }

Updated by phluid61 (Matthew Kerwin) over 1 year ago

Eregon (Benoit Daloze) wrote:

I'd prefer a syntax which is different than "key syntax without value", and refers to the variable name used for the value more clearly, like:

x = 1
y = 2
h = {x, y}

Please no, this is too close to perl's weird handling of lists/hashes. To me it reads like you're trying to write:

h = {1=>2}

Updated by shevegen (Robert A. Heiler) over 1 year ago

I agree with Matthew.

I understand the suggestion trying to make the syntax even more succinct
(less to type) but I think it's one step too much. Ruby already has a
quite condensed syntax.

I think this syntax here, asked by Benoit, is also problematic:

h = { "x" => , "y" => }

Has a slight "visual" problem, at the least to me. I would expect =>
to "point" to something on the right hand side, which the normal
syntax in hashes, in ruby, requires (unless you use the foo: :bar
syntax notation).

The:

h = {x:, y:}

to my brain it's indeed a bit confusing because I would normally
expect something on the right side of "foo: ".

Updated by shugo (Shugo Maeda) over 1 year ago

Eregon (Benoit Daloze) wrote:

I'd prefer a syntax which is different than "key syntax without value", and refers to the variable name used for the value more clearly, like:

x = 1
y = 2
h = {x, y}

I proposed the above syntax in #11105, but it was rejected, and this proposal is alternative.

Updated by matz (Yukihiro Matsumoto) over 1 year ago

I prefer this syntax to #11105, but this introduces a Ruby-specific syntax different from ES6 syntax.
Besides that, I don't like both anyway because they are not intuitive (for me).

Matz.

Updated by shugo (Shugo Maeda) over 1 year ago

  • Status changed from Open to Rejected

matz (Yukihiro Matsumoto) wrote:

I prefer this syntax to #11105, but this introduces a Ruby-specific syntax different from ES6 syntax.
Besides that, I don't like both anyway because they are not intuitive (for me).

So I withdraw this proposal.

Also available in: Atom PDF