From: "Lourens Naudé" Date: 2020-04-08T10:03:15+01:00 Subject: [ruby-core:97746] Re: [Ruby master Bug#16769] Struct.new(..., immutable: true) --===============0819316119== Content-Type: multipart/alternative; boundary="000000000000e7a2de05a2c3c53c" --000000000000e7a2de05a2c3c53c Content-Type: text/plain; charset="UTF-8" Nice. Also references this Java JEP https://openjdk.java.net/jeps/359 On Wed, 8 Apr 2020 at 10:01, wrote: > Issue #16769 has been reported by k0kubun (Takashi Kokubun). > > ---------------------------------------- > Bug #16769: Struct.new(..., immutable: true) > https://bugs.ruby-lang.org/issues/16769 > > * Author: k0kubun (Takashi Kokubun) > * Status: Open > * Priority: Normal > * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN > ---------------------------------------- > ## Background > We've discussed interface to pass Struct attributes (like `immutable: > true`, which is actually not added yet) at once. But I believe just adding > `immutable: true` alone is really helpful in various cases. Thus I've spun > out this ticket only for `immutable: true` from [Feature #16122]. > > ## Proposal > > ```rb > Post = Struct.new(:id, :name, immutable: true) > > post = Post.new(1, "hello world") > post.id = 2 # NoMethodError (undefined method `id=' for # id=1, name="hello world">) > ``` > > Given `immutable: true`, an instance returned by `.new` is frozen, and > writer methods are not defined. > > ## Use case > > * Allow using Struct's nice features when we need an immutable model, > instead of defining a normal class with `attr_reader`s and methods to > support the Struct's features. > * If it were a Struct, `to_s`, `inspect`, `==`, and a bunch of other > methods are nicely defined by default. Deconstructing a Struct on Pattern > Matching is also available. > * This level of support from the entire ecosystem may not be > available if it's just a third-party library. > * We could achieve a similar thing if we call `Post.new(...).freeze` or > override `#initialize` to call `freeze` inside it, but it is not fun and > feels like a workaround. > * Today I suggested to use Struct for a model class to take advantage > of the above benefits in a code review, but the implementation stuck with a > bare class with `attr_reader`s because the author didn't want writer > methods to be defined (of course we don't want to manually undef them from > a Struct class either) and calling `freeze` to workaround it seems tricky. > I strongly desired Ruby's Struct is useful enough to cover this use case. > > > > -- > https://bugs.ruby-lang.org/ > > Unsubscribe: > > --000000000000e7a2de05a2c3c53c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Nice. Also references this Java JEP=C2=A0

Issue #16769 has been reported by k0kubun (Takashi Koku= bun).

----------------------------------------
Bug #16769: Struct.new(..., immutable: true)
https://bugs.ruby-lang.org/issues/16769

* Author: k0kubun (Takashi Kokubun)
* Status: Open
* Priority: Normal
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
## Background
We've discussed interface to pass Struct attributes (like `immutable: t= rue`, which is actually not added yet) at once. But I believe just adding `= immutable: true` alone is really helpful in various cases. Thus I've sp= un out this ticket only for `immutable: true` from [Feature #16122].

## Proposal

```rb
Post =3D Struct.new(:id, :name, immutable: true)

post =3D Post.new(1, "hello world")
post.id= =3D 2 # NoMethodError (undefined method `id=3D' for #<struct Post i= d=3D1, name=3D"hello world">)
```

Given `immutable: true`, an instance returned by `.new` is frozen, and writ= er methods are not defined.

## Use case

* Allow using Struct's nice features when we need an immutable model, i= nstead of defining a normal class with `attr_reader`s and methods to suppor= t the Struct's features.
=C2=A0 * If it were a Struct, `to_s`, `inspect`, `=3D=3D`, and a bunch of o= ther methods are nicely defined by default. Deconstructing a Struct on Patt= ern Matching is also available.
=C2=A0 =C2=A0 =C2=A0* This level of support from the entire ecosystem may n= ot be available if it's just a third-party library.
=C2=A0 * We could achieve a similar thing if we call `Post.new(...).freeze`= or override `#initialize` to call `freeze` inside it, but it is not fun an= d feels like a workaround.
=C2=A0 =C2=A0 =C2=A0* Today I suggested to use Struct for a model class to = take advantage of the above benefits in a code review, but the implementati= on stuck with a bare class with `attr_reader`s because the author didn'= t want writer methods to be defined (of course we don't want to manuall= y undef them from a Struct class either) and calling `freeze` to workaround= it seems tricky. I strongly desired Ruby's Struct is useful enough to = cover this use case.



--
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=3Dunsubscribe= >
<http://lists.ruby-lang.org/cgi-bin/m= ailman/options/ruby-core>
--000000000000e7a2de05a2c3c53c-- --===============0819316119== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline Unsubscribe: --===============0819316119==--