Project

General

Profile

Actions

Bug #17719

open

Irregular evaluation order in hash literals

Added by nobu (Nobuyoshi Nakada) 3 months ago. Updated 3 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:102831]

Description

mame (Yusuke Endoh) pointed out an irregular evaluation order example.

$ ruby -e '{foo:p(1), bar:p(2), foo:p(3)}'
-e:1: warning: key :foo is duplicated and overwritten on line 1
1
3
2

It feels like a bug.
https://github.com/nobu/ruby/tree/duplicated-keys-order

Or, probably it would be better to turn into an error?

Updated by xtkoba (Tee KOBAYASHI) 3 months ago

To me the evaluation order of 1->3->2 is a bit unexpected, but it might be OK unless specified otherwise.

IMO, duplicate keys in hash literals should be allowed in a scripting language like Ruby. For example, they are explicitly allowed in another scripting language named Python [1]. On the other hand, compiled languages should disallow them.

[1] https://mail.python.org/pipermail/python-ideas/2019-March/055726.html

Actions

Also available in: Atom PDF