softwaremill / magnolia

Easy, fast, transparent generic derivation of typeclass instances
https://softwaremill.com/open-source/
Apache License 2.0
754 stars 115 forks source link

[scala2] feat: adding on-demand default evaluation #533

Closed Andrapyre closed 1 month ago

Andrapyre commented 1 month ago

Description

Currently, all default values are precompiled. This has led to random generators, such as random UUID.randomUUID() and Instant.now() being memoized (see https://github.com/zio/zio-json/issues/1055). This PR adds an option to evaluate default parameters on-demand. See the accompanying PR for Scala 3 support: https://github.com/softwaremill/magnolia/pull/534

adamw commented 1 month ago

Thanks for the PR! I'll take a detailed look in a couple of days, but for sure we'll need to find a way to keep bincompat. Or move these changes to v2.

joroKr21 commented 1 month ago

Since this would break new CallByNeed(param) and force users to use CallByNeed(param), I tried to find some examples of libs where users would need to make this change, but I couldn't find any instances outside of Magnolia where this implementation is actually being used. See this search. It seems that breaking this would likely have minimal effect, if any.

It can break when you have two dependencies that use Magnolia on the classpath, one the older version using the constructor, and another one the newer version using apply. You can add another constructor to keep binary compatibility.

Andrapyre commented 1 month ago

I've adjusted both PRs to keep bincompat. Build should be passing now.

adamw commented 1 month ago

Thanks for the PR!