New alias for Dry::Struct#new


I experimented with aliasing #new to | since it resembles the way Elm records and Elixir maps are updated:

p = Person[name: "Test", age: 42]
#=> #<Person name="Test" age=42>
p | { age: 43 }
#=> #<Person name="Test" age=43>

I kinda like it, what do you think?


I have to say, this is impressively concise! But I feel it’s perhaps a little too… not-idiomatic for the Ruby world (even for us!). I feel that this could also be potentially confusing given we’re already using | for logical operations in other dry-* gems (dry-validation and dry-logic).

I’m interested to hear: how often do you think you’d find yourself wanting to use #new, and for what reasons? It’s not something I’ve done all that much of in the apps I’m building, at least.


I did wonder about this, but then it’s actually somewhat in line with how it works for arrays:

[1,2,3] | [4]
#=> [1, 2, 3, 4]

For key-value-pairs I think it’s reasonable that elements with the same key would overwrite the existing value.

But you may have a good point, it’s not really that important, since for the rare cases one would want to do this new is perfectly sufficient. I guess this was all based on some ideas re state management I wanted to try to try to borrow from Elm and then a concise syntax like this would be nice.