Consider the following, which prints ‘calling full’ twice:
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
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.
Dry::Struct offer any mechanism by which I can, at one fell swoop, say “memoize all the methods I add this value object”?
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.