- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
simulating call-by-name with case classes?
Sun, 2011-12-04, 15:43
body
p { margin-bottom: 0cm; margin-top: 0pt; }
<console>:1: error: `val' parameters may not be call-by-name
case class Foo(s: => String)
ok, so I try creating an apply method in the companion object:
scala> :paste
// Entering paste mode (ctrl-D to finish)
case class Foo(s: () => String)
object Foo {def apply(s: => String) = new Foo(() => s)}
// Exiting paste mode, now interpreting.
<console>:7: error: double definition:
method apply:(s: () => String)Foo and
method apply:(s: => String)Foo at line 8
have same type after erasure: (s: Function0)$line2.$read#$iw#$iw#Foo
case class Foo(s: () => String)
^
Of course I can create a regular class, but the class I'm writing is really a value class. Is there another way to achieve what I want?
Regards,
Ittay
I want a case class to have a call-by-name argument to its constructor.
scala> case class Foo(s: => String)<console>:1: error: `val' parameters may not be call-by-name
case class Foo(s: => String)
ok, so I try creating an apply method in the companion object:
scala> :paste
// Entering paste mode (ctrl-D to finish)
case class Foo(s: () => String)
object Foo {def apply(s: => String) = new Foo(() => s)}
// Exiting paste mode, now interpreting.
<console>:7: error: double definition:
method apply:(s: () => String)Foo and
method apply:(s: => String)Foo at line 8
have same type after erasure: (s: Function0)$line2.$read#$iw#$iw#Foo
case class Foo(s: () => String)
^
Of course I can create a regular class, but the class I'm writing is really a value class. Is there another way to achieve what I want?
Regards,
Ittay
Ittay Dror wrote:
My current workaround is to use an implicit argument with a default value of null to disambiguate. Any nicer alternatives?