From: "zverok (Victor Shepelev) via ruby-core" Date: 2023-03-29T06:34:52+00:00 Subject: [ruby-core:113037] [Ruby master Feature#19555] Allow passing default options to `Data.define` Issue #19555 has been updated by zverok (Victor Shepelev). This is not simple as it seems (that's why we abstained of introducing something like that). Imagine this: ```ruby Point = Data.define(:x, :y, tags: []) p1 = Point.new(1, 2) p1.tags << 'foo' p2 = Point2.new(3, 4) p2.tags #=>? ``` In naive implementation, `p1.tags` is the same ary as passed for default, and `p1.tags << 'foo'` adjusts the default value. In less naive implementation, we need probably `.dup` defaults, which still might be not enough or impossible. Yes, mutable values are kinda against Data idea, but it doesn���t mean nobody will do that. ---------------------------------------- Feature #19555: Allow passing default options to `Data.define` https://bugs.ruby-lang.org/issues/19555#change-102574 * Author: p8 (Petrik de Heus) * Status: Open * Priority: Normal ---------------------------------------- Defining a subclass of `Data` with default attributes can currently be done by overriding `intialize`: ```ruby class Point < Data.define(:x, :y, :z) def initialize(x:, y:, z: 0) = super end p Point.new(1, 2) #=> # ``` It would be nice if we could do it in `define` as well: ```ruby Point = Data.define(:x, :y, z: 0) ``` -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/