From: zverok.offline@... Date: 2019-08-23T17:40:37+00:00 Subject: [ruby-core:94508] [Ruby master Feature#16122] Struct::Value: simple immutable value object Issue #16122 has been reported by zverok (Victor Shepelev). ---------------------------------------- Feature #16122: Struct::Value: simple immutable value object https://bugs.ruby-lang.org/issues/16122 * Author: zverok (Victor Shepelev) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- Currently, **Struct** behaves like a *value object*: * it is created from simple attributes; * its equality and representation based only on those attributes. But also, it behaves like a **mutable object** (allows to set attributes), and **as a kind-of collection** (includes `Enumerable`, has `to_a` and `[]` accessors). And while `Struct` is kinda useful as is, I found that in a lot of cases what I really *mean* creating a Struct is just creating a pure, immutable value object, that is *just it*. There are a lot of gems that go this or that far to provide "value object" concept, but in fact, the concept is so simple that typically nobody will install the gem to just have it -- that's why I believe it should be in language core. So, here is the proposal **and its C implementation:** * Class `Struct::Value`, which shares most of the implementation with a `Struct` (but is neither subclass nor superclass of it); * Unlike struct, it is: * Not Enumerable; * Immutable; * Doesn't think of itself as "almost hash" (doesn't have `to_a`, `values` and `[]` methods); * Can have empty members list (fun fact: `Struct.new('Foo')` creating member-less `Struct::Foo`, is allowed, but `Struct.new()` is not) to allow usage patterns like: ```ruby class MyService Success = Struct::Value.new(:results) NotFound = Struct::Value.new end ``` `NotFound` here, unlike, say, `Object.new.freeze` (another pattern for creating "empty typed value object"), has nice inspect `#`, and created consistently with the `Success`, making the code more readable. ---Files-------------------------------- struct_value.patch (18.6 KB) -- https://bugs.ruby-lang.org/ Unsubscribe: