# Dry::Struct auto-memoization?

**URL:** <https://discourse.dry-rb.org/t/dry-struct-auto-memoization/631>\
**Category:** Support\
**Created:** [September 30, 2018, 6:25pm UTC](https://discourse.dry-rb.org/t/dry-struct-auto-memoization/631 "2018-09-30T18:25:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonahx](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/jonahx/32/228_2.png) [@jonahx](https://discourse.dry-rb.org/u/jonahx)\
**Post date:** [September 30, 2018, 6:25pm UTC](https://discourse.dry-rb.org/t/dry-struct-auto-memoization/631/1 "2018-09-30T18:25:20Z")

</div>

Consider the following, which prints ‘calling full’ twice:

```auto
module Types
  include Dry::Types.module
  NonEmptyString = Types::Strict::String.constrained(format: /\S+/)
end

class Person < Dry::Struct
  attribute :first, Types::NonEmptyString
  attribute :last, Types::NonEmptyString
  def full
    puts 'calling full'
    first + ' ' + last
  end
end

p = Person.new(first: 'John', last: 'Smith')
p p.full
p p.full

```

Of course I manually memoize `full`, or do it with a gem like `memoist`, etc:

```auto
  def full
    return @full if @full
    puts 'calling full'
    @full = first + ' ' + last
  end

```

But with an immutable value object, this is redundant in a sense, because _any_ function of my immutable values is guaranteed by referential transparency to be safe to memoize.

While there may be some edge cases where you don’t want to memoize for reasons like memory consumption, you very often do.

Does `Dry::Struct` offer any mechanism by which I can, at one fell swoop, say “memoize all the methods I add this value object”?

Or is `Dry::Struct` intended only for the base data, preferring to put “derived” data like the method `full` above into decorator objects?

I would love to hear any thoughts on this.

---

<div class="post-metadata">

**Author:** ![timriley](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/timriley/32/25_2.png) [@timriley](https://discourse.dry-rb.org/u/timriley)\
**Post date:** [October 2, 2018, 4:33am UTC](https://discourse.dry-rb.org/t/dry-struct-auto-memoization/631/2 "2018-10-02T04:33:31Z")

</div>

@jonahx Personally, I have no problem with a method like this…

```ruby
  def full
    first + ' ' + last
  end

```

…being fully executed upon each invocation. It’s simply not a slow method. Memoization feels like overkill here.

I feel like having dry-struct automatically memoize ordinary methods added to the class would violate principle of least surprise. This is not how usual Ruby classes work, and I don’t think people would consider dry-struct’s purview to cover this, particularly when there are any number of memoizing libs already available (including [`Dry::Core::Memoizable`](https://github.com/dry-rb/dry-core/blob/master/spec/dry/core/memoizable_spec.rb)).

If you wanted all your structs to memoize all their methods, one way would be to create your own in-app struct superclass which inherits from `Dry::Struct` and also implements its own `.method_added` which can then add in the memoizing logic.

---

<div class="post-metadata">

**Author:** ![jonahx](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/jonahx/32/228_2.png) [@jonahx](https://discourse.dry-rb.org/u/jonahx)\
**Post date:** [October 3, 2018, 7:33pm UTC](https://discourse.dry-rb.org/t/dry-struct-auto-memoization/631/3 "2018-10-03T19:33:15Z")

</div>

Thanks @timriley.

> [@timriley](#):
>
> It’s simply not a slow method.

Yeah that one ofc doesn’t matter… it was merely illustrative of the concept. You can imagine cases where the computation is slow enough that you want memoization, and I think it doesn’t _hurt_ for cases like this.

> and I don’t think people would consider dry-struct’s purview to cover this

I was imagining it would be opt in.

> any number of memoizing libs already available (including [`Dry::Core::Memoizable`](https://github.com/dry-rb/dry-core/blob/master/spec/dry/core/memoizable_spec.rb)).

Had been looking at `memoist` but wasn’t aware `Dry` provided one, thanks for pointing this out.

> would be to create your own in-app struct superclass which inherits from `Dry::Struct` and also implements its own `.method_added` which can then add in the memoizing logic.

I like this, thanks.
