Bug #19259 closed
`Data#with` doesn't call `initialize` nor `initialize_copy`
Added by ko1 (Koichi Sasada) over 2 years ago.
Updated about 2 years ago.
Description
Data#with
doesn't call initialize
nor initialize_copy
.
It is confirmation request.
class P < Data . define ( :x , :y )
def initialize_copy ( ... )
p :initialize_copy
super
end
def initialize ( ... )
p :initialize
super
end
end
pt = P . new ( 1 , 2 )
#=> :initialize
pt . clone
#=> :initialize_copy
pt . dup
#=> :initialize_copy
pt . with ( x: 10 )
#=> N/A
For example, if an author of P
add validation code, #with
skips it.
Description updated (diff )
Description updated (diff )
In principle, it should call initialize
. But we will reconsider if any there's any concern (e.g., performance).
Matz.
Related to Feature #19000 : Data: Add "Copy with changes method" [Follow-on to #16122 Data: simple immutable value object] added
My expectation is it would call initialize
as well.
I didn't think about that in the review of https://github.com/ruby/ruby/pull/6766 , sorry.
So like:
class Data
def with ( ** changes )
self . class . new ( ** to_h , ** changes )
end
end
BTW, should .new
be called dynamically too?
BTW ruby/spec's mock/should_receive
should be useful to test whether initialize is called and with which arguments.
BTW, should .new be called dynamically too?
Intuitively, I don't think so, but I fail to find good logical arguments :)
(We can, for example, note that with
is some Data
-specific and fancy copying/duplication method, and dup
/clone
don't invoke .new
)
zverok (Victor Shepelev) wrote in #note-6 :
Intuitively, I don't think so, but I fail to find good logical arguments :)
(We can, for example, note that with
is some Data
-specific and fancy copying/duplication method, and dup
/clone
don't invoke .new
)
Yeah, I think it's uncommon to call .new
dynamically for core classes so probably no need here.
It would matter if someone defined a custom new
, but our expectation with Data is that people define a custom initialize and not a custom new.
Status changed from Open to Closed
Also available in: Atom
PDF
Like 0
Like 0 Like 0 Like 0 Like 0 Like 1 Like 0 Like 0 Like 0 Like 0