From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-3.9 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 311031F461 for ; Thu, 29 Aug 2019 12:50:35 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 52D2C120C58; Thu, 29 Aug 2019 21:50:26 +0900 (JST) Received: from o1678916x28.outbound-mail.sendgrid.net (o1678916x28.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 6F4B7120AAE for ; Thu, 29 Aug 2019 21:50:23 +0900 (JST) Received: by filter0058p3las1.sendgrid.net with SMTP id filter0058p3las1-3897-5D67CA0E-49 2019-08-29 12:50:22.388834202 +0000 UTC m=+231388.238363009 Received: from herokuapp.com (unknown [54.227.36.202]) by ismtpd0029p1mdw1.sendgrid.net (SG) with ESMTP id klc793doSaeAqwsoA5GyZg for ; Thu, 29 Aug 2019 12:50:22.160 +0000 (UTC) Date: Thu, 29 Aug 2019 12:50:22 +0000 (UTC) From: daniel@dan42.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 70217 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16122 X-Redmine-Issue-Author: zverok X-Redmine-Sender: Dan0042 X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?8sy4RigFvRTdBfCVJrT9zb2J88PC92TMQwdNgaWYaq59W7mZiUYARaWUFMbbjw?= =?us-ascii?Q?f=2FaARp9Xg+Eesk4p4HxNQhSGAw6X7VcZXGNDH5m?= =?us-ascii?Q?m98=2F6YT86qsFafAXDagzjHUMfXCP0QKqHHO3QQp?= =?us-ascii?Q?LIvc+c35RaU+fexqXJEMbbsNkiardmwzS9etHiv?= =?us-ascii?Q?EmJ3RIrkLAwLu=2FFKUnKG9pberhXewWzGurg=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 94665 Subject: [ruby-core:94665] [Ruby master Feature#16122] Struct::Value: simple immutable value object X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #16122 has been updated by Dan0042 (Daniel DeLorme). If I understand correctly, the idea is to have `X=Struct::Value.new(:x,:y,:z)` which is strictly equivalent to ```ruby class X def initialize(x=nil, y=nil, z=nil) @x,@y,@z = x,y,z end attr_reader :x, :y, :z end ``` Or was there some nuance I didn't catch? ---------------------------------------- Feature #16122: Struct::Value: simple immutable value object https://bugs.ruby-lang.org/issues/16122#change-81269 * Author: zverok (Victor Shepelev) * Status: Feedback * 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/