Skip to content

Commit bf83db5

Browse files
author
Oron Port
authored
GetArg lazy return value to support by-name arguments (#143)
GetArg lazy return value to support by-name arguments
1 parent 18d6996 commit bf83db5

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/main/scala/singleton/ops/impl/GeneralMacros.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,9 +726,9 @@ trait GeneralMacros {
726726
new $opTpe {
727727
type OutWide = $outTpe
728728
type Out = $outTpe
729-
final val value: $outTpe = $valueTree
729+
final lazy val value: $outTpe = $valueTree
730730
final val isLiteral = false
731-
final val valueWide: $outTpe = $valueTree
731+
final lazy val valueWide: $outTpe = $valueTree
732732
}
733733
"""
734734
case None =>

src/test/scala/singleton/ops/GetArgSpec.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ class GetArgSpec extends Properties("GetArgSpec") {
3333
1.extendable
3434
}
3535

36+
object ByName
37+
def byName(arg : => Any)(implicit getArg : GetArg0) = getArg
38+
property("By name argument not evaluated until value is read") = {
39+
var byNameNotRead = true
40+
val b = byName({byNameNotRead = false; ByName})
41+
val preRead = byNameNotRead
42+
val forceRead = b.value //now lazily invoked
43+
preRead && !byNameNotRead
44+
}
3645

3746
val one = 1
3847
val two = 2

0 commit comments

Comments
 (0)