ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:109853] [Ruby master Feature#19001] Data: Add #to_h symmetric to constructor with keyword args [Follow-on to #16122 Data: simple immutable value object]
@ 2022-09-08 18:37 RubyBugs (A Nonymous)
  2022-09-08 18:44 ` [ruby-core:109854] " RubyBugs (A Nonymous)
  2022-09-10 13:24 ` [ruby-core:109873] " zverok (Victor Shepelev)
  0 siblings, 2 replies; 3+ messages in thread
From: RubyBugs (A Nonymous) @ 2022-09-08 18:37 UTC (permalink / raw)
  To: ruby-core

Issue #19001 has been reported by RubyBugs (A Nonymous).

----------------------------------------
Feature #19001: Data: Add #to_h symmetric to constructor with keyword args  [Follow-on to #16122 Data: simple immutable value object]
https://bugs.ruby-lang.org/issues/19001

* Author: RubyBugs (A Nonymous)
* Status: Open
* Priority: Normal
----------------------------------------
*Extracted a follow-up to [#16122 Data: simple immutable value object](https://bugs.ruby-lang.org/issues/16122)*

# 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:

```ruby
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](https://rubygems.org/gems/values)

## Alternatives

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



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:109854] [Ruby master Feature#19001] Data: Add #to_h symmetric to constructor with keyword args [Follow-on to #16122 Data: simple immutable value object]
  2022-09-08 18:37 [ruby-core:109853] [Ruby master Feature#19001] Data: Add #to_h symmetric to constructor with keyword args [Follow-on to #16122 Data: simple immutable value object] RubyBugs (A Nonymous)
@ 2022-09-08 18:44 ` RubyBugs (A Nonymous)
  2022-09-10 13:24 ` [ruby-core:109873] " zverok (Victor Shepelev)
  1 sibling, 0 replies; 3+ messages in thread
From: RubyBugs (A Nonymous) @ 2022-09-08 18:44 UTC (permalink / raw)
  To: ruby-core

Issue #19001 has been updated by RubyBugs (A Nonymous).


Per @Matz [here](https://bugs.ruby-lang.org/issues/16122#note-51), the preference would be for the constructor to take either:
* Only keyword args
* Either keyword args OR positional args

The [Values gem](https://github.com/ms-ati/Values) 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:

```ruby
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)
```

----------------------------------------
Feature #19001: Data: Add #to_h symmetric to constructor with keyword args  [Follow-on to #16122 Data: simple immutable value object]
https://bugs.ruby-lang.org/issues/19001#change-99091

* Author: RubyBugs (A Nonymous)
* Status: Open
* Priority: Normal
----------------------------------------
*Extracted a follow-up to [#16122 Data: simple immutable value object](https://bugs.ruby-lang.org/issues/16122)*

# 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:

```ruby
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](https://rubygems.org/gems/values)

## Alternatives

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



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:109873] [Ruby master Feature#19001] Data: Add #to_h symmetric to constructor with keyword args [Follow-on to #16122 Data: simple immutable value object]
  2022-09-08 18:37 [ruby-core:109853] [Ruby master Feature#19001] Data: Add #to_h symmetric to constructor with keyword args [Follow-on to #16122 Data: simple immutable value object] RubyBugs (A Nonymous)
  2022-09-08 18:44 ` [ruby-core:109854] " RubyBugs (A Nonymous)
@ 2022-09-10 13:24 ` zverok (Victor Shepelev)
  1 sibling, 0 replies; 3+ messages in thread
From: zverok (Victor Shepelev) @ 2022-09-10 13:24 UTC (permalink / raw)
  To: ruby-core

Issue #19001 has been updated by zverok (Victor Shepelev).


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:
```ruby
Point = Struct.new(:x, :y, :z)

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


----------------------------------------
Feature #19001: Data: Add #to_h symmetric to constructor with keyword args  [Follow-on to #16122 Data: simple immutable value object]
https://bugs.ruby-lang.org/issues/19001#change-99113

* Author: RubyBugs (A Nonymous)
* Status: Open
* Priority: Normal
----------------------------------------
*Extracted a follow-up to [#16122 Data: simple immutable value object](https://bugs.ruby-lang.org/issues/16122)*

# 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:

```ruby
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](https://rubygems.org/gems/values)

## Alternatives

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



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-09-10 13:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-08 18:37 [ruby-core:109853] [Ruby master Feature#19001] Data: Add #to_h symmetric to constructor with keyword args [Follow-on to #16122 Data: simple immutable value object] RubyBugs (A Nonymous)
2022-09-08 18:44 ` [ruby-core:109854] " RubyBugs (A Nonymous)
2022-09-10 13:24 ` [ruby-core:109873] " zverok (Victor Shepelev)

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).