title: Scala: groovy with analogue
body: <p>Groovy has awesome syntax sugar called <code>with</code>. For example:</p>
  
  <pre><code>@groovy.transform.ToString 
  class Person {
    String name
    int age
  }
  
  def p = new Person()
  p.with {
    name = "Joe"
    age  = 10
  }
  
  println(p)
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Person(Joe, 10)
  </code></pre>
  
  <p>Is there any chance to achieve same behavior in Scala?</p>
  
  <p>Yes, I can import all properties of specific variable like this:</p>
  
  <pre><code>scala&gt; case class Person(name: String, age: Int)
  defined class Person
  
  scala&gt; val p = Person("Joe", 10)
  p: Person = Person(Joe,10)
  
  scala&gt; import p._
  import p._
  
  scala&gt; name
  res0: String = Joe
  
  scala&gt; age
  res1: Int = 10
  </code></pre>
  
  <p>But this approach has disadvantages - scope is too wide and I don't want to wrap it into code block</p>
  
  <p>Something similar is described here: <a href="http://meta.plasm.us/posts/2013/08/30/horrible-code/" rel="nofollow noreferrer">http://meta.plasm.us/posts/2013/08/30/horrible-code/</a></p>
  
  <hr>
  
  <p>You can try this Groovy code here: <a href="https://groovyconsole.appspot.com/script/5071841047609344" rel="nofollow noreferrer">groovyconsole.appspot.com</a></p>