From: takashikkbn@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:102147] [Ruby master Feature#16806] Struct#initialize accepts keyword arguments too by default
Date: Tue, 19 Jan 2021 00:05:11 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-89999.20210119000511.10073@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16806.20200422061459.10073@ruby-lang.org
Issue #16806 has been updated by k0kubun (Takashi Kokubun).
Assignee set to k0kubun (Takashi Kokubun)
Status changed from Closed to Assigned
----------------------------------------
Feature #16806: Struct#initialize accepts keyword arguments too by default
https://bugs.ruby-lang.org/issues/16806#change-89999
* Author: k0kubun (Takashi Kokubun)
* Status: Assigned
* Priority: Normal
* Assignee: k0kubun (Takashi Kokubun)
----------------------------------------
## Proposal
```rb
Post = Struct.new(:id, :name)
# In addition to this,
Post.new(1, "hello") #=> #<struct Post id=1, name="hello">
# Let the following initialization also work
Post.new(id: 1, name: "hello") #=> #<struct Post id=1, name="hello">
```
### Known incompatibility
* `Post.new(id: 1, name: "hello")` will be `#<struct Post id=1, name="hello">` instead of `#<struct Post id={:id=>1, :name=>"hello"}, name=nil>`
* Struct initialization only using keyword arguments should be warned in Ruby 3.0. **This feature should be introduced in Ruby 3.1 or later.**
### Edge cases
* When keyword arguments and positional arguments are mixed: `Post.new(1, name: "hello")`
* This should continue to work like Ruby 2: `#<struct Post id=1, name={:name="hello"}>`
* Only keywords are given but they include an invalid member: `Post.new(foo: "bar")`
* ArgumentError (unknown keywords: foo)
* When `keyword_init` is used
* nil: default behavior. Positional arguments given use positional init. Keyword arguments without positional arguments treated as positional in 3.0 with warning, and treated as keyword init in Ruby 3.1.
* true: Require keyword init, disallow positional init.
* false: Treat keywords as positional hash.
## Use cases
* Simplify a struct definition where [Feature #11925] is used.
* When we introduced [Feature #11925], @mame thought we don't need `keyword_init: true` once keyword args are separated (https://docs.google.com/document/d/1XbUbch8_eTqh21FOwj9a_X-ZyJyCBjxkq8rWwfpf5BM/edit#). That's what this ticket is all about.
* However, the keyword arguments separation was done differently from what we expected at the moment. So we need to deal with the "Known incompatibility".
* Matz objected to having a new keyword argument (`immutable: true`) in `Struct.new` at https://bugs.ruby-lang.org/issues/16769#note-8. So `keyword_init: true` seems also against Ruby's design. Now we should be able to skip specifying the option for consistency in the language design.
--
https://bugs.ruby-lang.org/
next prev parent reply other threads:[~2021-01-19 0:05 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-22 6:14 [ruby-core:98014] [Ruby master Feature#16806] Struct#initialize accepts keyword arguments too by default takashikkbn
2020-04-22 14:57 ` [ruby-core:98019] " merch-redmine
2020-04-23 6:10 ` [ruby-core:98024] " takashikkbn
2021-01-13 6:30 ` [ruby-core:102052] " matz
2021-01-19 0:05 ` takashikkbn [this message]
2022-01-29 8:29 ` [ruby-core:107343] " k0kubun (Takashi Kokubun)
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.journal-89999.20210119000511.10073@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).