Project

General

Profile

Actions

Feature #19001

closed

Data: Add #to_h symmetric to constructor with keyword args [Follow-on to #16122 Data: simple immutable value object]

Added by RubyBugs (A Nonymous) over 2 years ago. Updated 7 months ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:109853]

Description

Extracted a follow-up to #16122 Data: simple immutable value object

Proposal: Add a #to_h method symmetric to a constructor accepting keyword arguments

This allows round-trip between a Hash and a Value object instance, for example:

Point = Data.define(:x, :y, :z)

points = [
  Point.new(x: 1, y: 0, z: 0),
  Point.new(x: 0, y: 1, z: 0),
  Point.new(x: 0, y: 0, z: 1),
]

hashes = points.map(&:to_h)

points_2 = hashes.map { |h| Point.new(**h) }

points_2 == points
#=> true

Why?

Having symmetric operation between #to_h and a keyword-args constructor is a major ergonomic factor in usage of immutable value objects.

To play with code that works like this, you may take a look at the Values gem

Alternatives

If there is no symmetric construction and de-construction along these lines, a number of use cases become more complicated and less ergonomic.

Updated by RubyBugs (A Nonymous) over 2 years ago

Per @matz (Yukihiro Matsumoto) here, the preference would be for the constructor to take either:

  • Only keyword args
  • Either keyword args OR positional args

The Values gem provides separate positional and keyword args constructors:

  • .new -- positional constructor
  • .with -- keyword args constructor

Given the high need for ergonomics related to the keyword args constructor, my recommendation is that this one is more important than a positional constructor.

For example, when using simple immutable value objects, a keyword args constructor combined with a #with method to copy an instance with discrete changes (see #19000), can completely replace the need for default values, as follows:

Point = Data.define(:x, :y, :z)

Default = Point.new(x: 1, y: 2, z: 3)

p = Default.with(x: 42)
#=> Point(x: 42, y: 2, z: 3)

Updated by zverok (Victor Shepelev) over 2 years ago

There isn't any need for this ticket as a separate request, as far as I am concerned.
It works in the initial implementation of data already as submitted in https://bugs.ruby-lang.org/issues/16122#note-68, and even works with ol' good Struct, too:

Point = Struct.new(:x, :y, :z)

Point[1, 0, 0].to_h.then { Point[**_1] } == Point[1, 0, 0] # => true

Updated by RubyBugs (A Nonymous) 7 months ago

Agreed, this need ended up being met by the initial implementation! Thank you, closing this ticket.

zverok (Victor Shepelev) wrote in #note-2:

There isn't any need for this ticket as a separate request, as far as I am concerned.
It works in the initial implementation of data already as submitted in https://bugs.ruby-lang.org/issues/16122#note-68, and even works with ol' good Struct, too:

Point = Struct.new(:x, :y, :z)

Point[1, 0, 0].to_h.then { Point[**_1] } == Point[1, 0, 0] # => true
Actions #4

Updated by hsbt (Hiroshi SHIBATA) 7 months ago

  • Status changed from Open to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0