Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This gets you almost there. There is still a Map I don't get easily rid of.</p> <p>The basic approach is to have a somewhat artificial parameter types, which inherit from a common type. This way the apply method just takes a single vararg.</p> <p>Using implicit conversion method I get rid of the ugly constructors for the parameter types</p> <pre><code>case class Child case class Person(stringParms: Map[String, String], mapParms: Map[String, Map[String, String]], children: List[Child]) { } sealed abstract class PersonParameter case class MapParameter(tupel: (String, Map[String, String])) extends PersonParameter case class StringParameter(tupel: (String, String)) extends PersonParameter case class ChildParameter(child: Child) extends PersonParameter object Person { def apply(params: PersonParameter*): Person = { var stringParms = Map[String, String]() var mapParms = Map[String, Map[String, String]]() var children = List[Child]() for (p ← params) { p match { case StringParameter(t) ⇒ stringParms += t case MapParameter(t) ⇒ mapParms += t case ChildParameter(c) ⇒ children = c :: children } } new Person(stringParms, mapParms, children) } implicit def tupel2StringParameter(t: (String, String)) = StringParameter(t) implicit def child2ChildParameter(c: Child) = ChildParameter(c) implicit def map2MapParameter(t: (String, Map[String, String])) = MapParameter(t) def main(args: Array[String]) { val firstChild = Child() val secondChild = Child() val bob: Person = Person("name" -&gt; "Bob","pets" -&gt; Map("cat" -&gt; "Mittens", "dog" -&gt; "Spot"),"status" -&gt; "asleep", firstChild, secondChild) println(bob) } } </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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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