smithy4s icon indicating copy to clipboard operation
smithy4s copied to clipboard

Memoize default values in Schema, not Field

Open kubukoz opened this issue 10 months ago • 3 comments

Surely this won't break anything...

PR Checklist (not all items are relevant to all PRs)

  • [ ] Added unit-tests (for runtime code)
  • [ ] Added bootstrapped code + smoke tests (when the rendering logic is modified)
  • [ ] Added build-plugins integration tests (when reflection loading is required at codegen-time)
  • [ ] Added alloy compliance tests (when simpleRestJson protocol behaviour is expanded/updated)
  • [ ] Updated dynamic module to match generated-code behaviour
  • [ ] Added documentation
  • [ ] Updated changelog

kubukoz avatar Feb 19 '25 22:02 kubukoz

can you elaborate on the why ?

Baccata avatar Feb 20 '25 08:02 Baccata

I would prefer to have Option[() => A] instead (kind of evaluator function without any memorization) because instantiation of default values can be side effecting or the result value could be just mutable and should not be shared.

plokhotnyuk avatar Feb 20 '25 08:02 plokhotnyuk

can you elaborate on the why ?

@Baccata I think it could be surprising that default values would get memoized in one place but not in another. My assumption is that the current behavior may not have been intentional, but maybe that's mistaken

I would prefer to have Option[() => A] instead (kind of evaluator function without any memorization) because instantiation of default values can be side effecting or the result value could be just mutable and should not be shared.

there should be no side effects in instantiation of defaults from generated code. The only way I see it happening would be if you use a refinement to a mutable type, and that's probably already broken in several ways...

kubukoz avatar Feb 20 '25 18:02 kubukoz