Skip to content

Commit b231259

Browse files
committed
by-value API
1 parent 3a1f747 commit b231259

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

core/src/main/scala/magnolia1/impl.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ object CaseClassDerivation:
114114
override def apply(): Option[p] = defaults.get(label).flatten.flatMap(d => unsafeCast(d.apply))
115115
paramFromMaps[Typeclass, A, p](
116116
label,
117-
new CallByNeed(tc),
118-
new CallByNeed(d, () => true),
117+
CallByNeed.createLazy(tc),
118+
CallByNeed.createValueEvaluator(d),
119119
repeated,
120120
annotations,
121121
inheritedAnnotations,
@@ -203,7 +203,7 @@ trait SealedTraitDerivation:
203203
IArray.from(paramTypeAnns[A]),
204204
isObject[s],
205205
idx,
206-
new CallByNeed(tc),
206+
CallByNeed.createLazy(tc),
207207
isType,
208208
asType
209209
)

core/src/main/scala/magnolia1/interface.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,17 +363,32 @@ object CallByNeed:
363363
/** Initializes a class that allows for suspending evaluation of a value until it is needed. Evaluation of a value via `.value` can only
364364
* happen once.
365365
*/
366+
def createLazy[A](a: () => A): CallByNeed[A] = new CallByNeed(a, () => false)
367+
368+
/** Initializes a class that allows for suspending evaluation of a value until it is needed. Evaluation of a value via `.value` can only
369+
* happen once.
370+
*
371+
* If by-name parameter causes serialization issue, use [[createLazy]].
372+
*/
366373
def apply[A](a: => A): CallByNeed[A] = new CallByNeed(() => a, () => false)
367374

368375
/** Initializes a class that allows for suspending evaluation of a value until it is needed. Evaluation of a value via `.value` can only
369376
* happen once. Evaluation of a value via `.valueEvaluator.map(evaluator => evaluator())` will happen every time the evaluator is called
370377
*/
378+
def createValueEvaluator[A](a: () => A): CallByNeed[A] = new CallByNeed(a, () => true)
379+
380+
/** Initializes a class that allows for suspending evaluation of a value until it is needed. Evaluation of a value via `.value` can only
381+
* happen once. Evaluation of a value via `.valueEvaluator.map(evaluator => evaluator())` will happen every time the evaluator is called
382+
*
383+
* If by-name parameter causes serialization issue, use [[withValueEvaluator]].
384+
*/
371385
def withValueEvaluator[A](a: => A): CallByNeed[A] = new CallByNeed(() => a, () => true)
372386
end CallByNeed
373387

374388
// Both params are later nullified to reduce overhead and increase performance.
375389
// The supportDynamicValueEvaluation is passed as a function so that it can be nullified. Otherwise, there is no need for the function value.
376-
final class CallByNeed[+A](private[this] var eval: () => A, private var supportDynamicValueEvaluation: () => Boolean) extends Serializable {
390+
final class CallByNeed[+A] private (private[this] var eval: () => A, private var supportDynamicValueEvaluation: () => Boolean)
391+
extends Serializable {
377392

378393
// This second constructor is necessary to support backwards compatibility for v1.3.6 and earlier
379394
def this(eval: () => A) = this(eval, () => false)

0 commit comments

Comments
 (0)