Project

General

Profile

Actions

Feature #15076

closed

Struct to raise error when keyword arguments used but not enabled

Added by macdevign (Macdevign mac) about 6 years ago. Updated about 6 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:88855]

Description

When Struct is used to instantiate object with keyword arguments disabled and yet if instantiated using keyword arguments, the instance fields will be set incorrectly.

Will it be clearer if a error is raised instead rather than assigning data incorrectly ? This is helpful and save time in nailing such issue in a large code base when there is mixture of Struct define with and without keyword arguments.

For example,

Info = Struct.new(:name, :country)  # without keyword argument enabled
c = Info.new(name: "myname", country: "Japan") # Instantiate with keywords

The c.name contains hash, and c.country is nil


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #11925: Struct construction with kwargsClosedActions
Actions #1

Updated by nobu (Nobuyoshi Nakada) about 6 years ago

Updated by nobu (Nobuyoshi Nakada) about 6 years ago

  • Status changed from Open to Rejected
Info = Struct.new(:name, :country, keyword_init: true)
c = Info.new(name: "myname", country: "Japan")
p c #=> #<struct Info name="myname", country="Japan">

Updated by k0kubun (Takashi Kokubun) about 6 years ago

Will it be clearer if a error is raised instead rather than assigning data incorrectly ?

With the current keyword arguments implementation (see also: [Feature #14183]), we can't distinguish the mistaken situation with one that we really wanted to initialize only the first argument with Symbol-key Hash. So that would be breaking change which we basically shouldn't introduce.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0