{"id":159,"date":"2012-05-28T18:11:52","date_gmt":"2012-05-28T22:11:52","guid":{"rendered":"http:\/\/localhost\/wordpress\/?p=159"},"modified":"2012-05-29T19:55:27","modified_gmt":"2012-05-29T23:55:27","slug":"using-groovy-closures-as-scala-functions","status":"publish","type":"post","link":"https:\/\/sourcedelica.com\/blog\/2012\/05\/using-groovy-closures-as-scala-functions\/","title":{"rendered":"Using Groovy Closures as Scala Functions"},"content":{"rendered":"<p>I have a Scala trait for persistence and transaction management (which I will blog about in more detail later). The trait looks like:<\/p>\n<pre class=\"brush: scala\">trait DomainManager {\r\n  def get[E](id: Long)(implicit m: ClassManifest[E]): Option[E]\r\n\r\n  def find[E](namedQuery: String, params: Map[String, Any] = null): Option[E]\r\n\r\n  \/\/...more methods\r\n\r\n  def withTransaction[R](f: (TransactionStatus) =&gt; R,\r\n            readOnly: Boolean = false,\r\n            propagationBehavior: PropagationBehavior = PropagationRequired): R\r\n}<\/pre>\n<p>Let&#8217;s take a look at <code>withTransaction()<\/code> specifically.   It is called like:<\/p>\n<pre class=\"brush: scala\">val result = domainManager.withTransaction { txStatus =&gt;\r\n  \/\/ Access database here\r\n}<\/pre>\n<p>If your application is written in Scala or Java it is sometimes handy to have certain pieces of it written in Groovy, to be able to easily change a class and reload it without restarting the application. Your Groovy code will be able to call any of your Java classes, but what about your Scala classes? For example, what if we want to call <code>withTransaction()<\/code> on <code>DomainManager<\/code>? How do we deal with the <code>f<\/code> parameter? And what about the default parameter values?<\/p>\n<p>Groovy and Scala both have the concept of functions as first class objects. In Groovy they are called closures and they are implemented by the class <a href=\"http:\/\/groovy.codehaus.org\/api\/groovy\/lang\/Closure.html\"><code>groovy.lang.Closure<\/code><\/a>. In Scala they are called functions and they are implemented by the traits <code>scala.Function0, <a href=\"http:\/\/www.scala-lang.org\/api\/current\/index.html#scala.Function1\">scala.Function1<\/a>, ... scala.FunctionN<\/code>, where N is the number of parameters to the function.<\/p>\n<p>In <code>withTransaction()<\/code> the type of <code>f<\/code> is <code>Function1[TransactionStatus, R]<\/code>. \u00a0It is a function that takes one parameter of type <code>TransactionStatus<\/code> and returns a generic <code>R<\/code>. \u00a0\u00a0<\/p>\n<p>Through Groovy magic <a href=\"http:\/\/groovy.codehaus.org\/Groovy+way+to+implement+interfaces\">closures can be coerced to arbitrary interfaces<\/a>. For example, I wrapped the <code>withTransaction()<\/code> method in Groovy like this:<\/p>\n<pre class=\"brush: groovy\">    def withTransaction(Closure closure) {\r\n        domainManager.withTransaction(closure as Function1&lt;TransactionStatus, Object&gt;,\r\n             false, PropagationRequired)\r\n    }<\/pre>\n<p>Here the <code>closure<\/code> parameter is coerced (using the Groovy <code>as<\/code> coercion operator) to a Scala <code>Function1[TransactionStatus, _]<\/code> which is the proper type after the generic parameter <code>R<\/code> is erased.<\/p>\n<p>You cannot use Scala&#8217;s default parameter values in Groovy or Java so the last two parameters (<code>readOnly<\/code> and <code>propagationBehavior<\/code>) need to be passed explicitly.<\/p>\n<p>Now I can call my Groovy\u00a0<code>withTransaction()<\/code> with a closure like:<\/p>\n<pre class=\"brush: groovy\">  def result = withTransaction { txStatus -&gt;\r\n    \/\/ Access database here\r\n  }<\/pre>\n<p>The same technique can be used to call Scala collection methods like <code>List.map()<\/code> with Groovy closures.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a Scala trait for persistence and transaction management (which I will blog about in more detail later). The trait looks like: trait DomainManager { def get[E](id: Long)(implicit m: ClassManifest[E]): Option[E] def find[E](namedQuery: String, params: Map[String, Any] = null): Option[E] \/\/&#8230;more methods def withTransaction[R](f: (TransactionStatus) =&gt; R, readOnly: Boolean = false, propagationBehavior: PropagationBehavior = [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[],"class_list":["post-159","post","type-post","status-publish","format-standard","hentry","category-groovy","category-scala","entry"],"_links":{"self":[{"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/posts\/159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/comments?post=159"}],"version-history":[{"count":61,"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/posts\/159\/revisions"}],"predecessor-version":[{"id":222,"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/posts\/159\/revisions\/222"}],"wp:attachment":[{"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/media?parent=159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/categories?post=159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sourcedelica.com\/blog\/wp-json\/wp\/v2\/tags?post=159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}