Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why not make <code>info</code> a case class argument as well?</p> <pre><code>case class One(someParam: String, info: Option[Info]) </code></pre> <p>You could implement a default value for <code>info</code> by defining the companion object like</p> <pre><code>object One { def apply(someParam: String): One = One(someParam, someSmartService.getInformationForSomething(someParam)) } </code></pre> <p>That would allow you to work with <code>One</code>s as follows:</p> <pre><code>One("foo") One("foo", Some(...)) One(encryptedSomeParam, encryptedInfo) One("plaintext").copy(someParam = encryptedSomeParam, info = encryptedInfo) </code></pre> <p><strong>EDIT 1: Lazy info</strong></p> <p>Case classes cannot have lazy val arguments, i.e., neither <code>info: =&gt; Option[String]</code> nor <code>lazy val info: Option[String]</code> is allowed as an argument type.</p> <p>You could make <code>info</code> a parameter-less function, though</p> <pre><code>case class One(someParam: String, info: () =&gt; Option[String]) object One { def apply(someParam: String): One = One(someParam, () =&gt; Some(someParam)) } </code></pre> <p>and then use it as</p> <pre><code>One("hi", () =&gt; Some("foo")) println(One("hi", () =&gt; None).info()) </code></pre> <p>This is obviously not ideal since it is not possible to introduce these changes without breaking code client code. Better solutions are welcome.</p> <p><strong>EDIT 2: Lazy info, no case class</strong></p> <p>If you don't insist on <code>One</code> being a case class (for example, because you really need <code>copy</code>), you could use a regular class with lazy values and a companion object for easy use:</p> <pre><code>class One(_someParam: String, _info: =&gt; Option[String]) { val someParam = _someParam lazy val info = _info } object One { def apply(someParam: String): One = new One(someParam, Await.result(...)) def apply(someParam: String, info: =&gt; Option[String]): One = new One(someParam, info) def unapply(one: One) = Some((one.someParam, one.info)) } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload