{"id":48,"date":"2011-06-05T23:49:11","date_gmt":"2011-06-06T06:49:11","guid":{"rendered":"http:\/\/www.lorrin.org\/blog\/?p=48"},"modified":"2012-05-24T10:48:18","modified_gmt":"2012-05-24T17:48:18","slug":"misadventures-in-breaking-actors-with-scala-self-types","status":"publish","type":"post","link":"https:\/\/www.lorrin.org\/blog\/2011\/06\/05\/misadventures-in-breaking-actors-with-scala-self-types\/","title":{"rendered":"Misadventures in Breaking Actors with Scala Self-Types"},"content":{"rendered":"<p>Quick refresher: <a href=\"http:\/\/www.scala-lang.org\/node\/124\">self-types<\/a> are commonly used when writing traits that want to proscribe that they get mixed in to a particular class. For example, the <a href=\"http:\/\/jonasboner.com\/2008\/10\/06\/real-world-scala-dependency-injection-di\">cake-pattern<\/a> leverages them. In the example below, <code>FooTrait<\/code> specifies a self-type of <code>FooTraitConfiguration<\/code> to insure that it is mixed in to a class that provides the expected <code>times<\/code> val.<\/p>\n<pre class=\"brush:scala\">import actors.Actor\r\nimport actors.Actor._\r\n\r\ntrait FooTraitConfiguration { val times : Int }\r\n\r\ntrait FooTrait { self:FooTraitConfiguration =&gt;\r\n  case object Ping\r\n  case object Pong\r\n\r\n  val a = actor {\r\n    loop {\r\n      react {\r\n        case Ping =&gt;\r\n          self ! Pong\r\n        case Pong =&gt;\r\n          for(_ &lt;- (1 to times)) { print(\".\") }\r\n          System.out.println(\"pong.\")\r\n  } } }\r\n\r\n  def ping = a ! Ping\r\n  def pong = a ! Pong\r\n}\r\n\r\nclass Foo extends FooTrait with FooTraitConfiguration { @Override val times = 5 }<\/pre>\n<p>But, alas, this fails to compile:<\/p>\n<pre>error: value ! is not a member of FooTrait with FooTraitConfiguration\r\nself ! Pong<\/pre>\n<p>It seems that the self-trait has broken the Actor API! And indeed, it has. Because self-traits are not usually specified with <code>self<\/code>! It should have been:<br \/>\n<code><strong>this<\/strong>:FooTraitConfiguration =&gt;<\/code><\/p>\n<p>The self-type means that within <code>FooTrait<\/code> the type of <code>this<\/code> is considered to be <code>FooTrait with FooTraitConfiguration<\/code>. Using a word other than <code>this<\/code> <a href=\"http:\/\/stackoverflow.com\/questions\/4017357\/difference-between-this-and-self-in-self-type-annotations\/4018995#4018995\">additionally sets up an alias to that type for e.g. use within nested classes<\/a>. And there&#8217;s the rub: Actors depend on a method named <code>self<\/code> which is shadowed when the alias to the type is named <code>self<\/code>.<\/p>\n<p>Note to self: <strong>Don&#8217;t use <code>self<\/code> when specifying self types!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quick refresher: self-types are commonly used when writing traits that want to proscribe that they get mixed in to a particular class. For example, the cake-pattern leverages them. In the example below, FooTrait specifies a self-type of FooTraitConfiguration to insure that it is mixed in to a class that provides the expected times val. import <a href='https:\/\/www.lorrin.org\/blog\/2011\/06\/05\/misadventures-in-breaking-actors-with-scala-self-types\/' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[48],"tags":[18,19],"_links":{"self":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts\/48"}],"collection":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":6,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":186,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts\/48\/revisions\/186"}],"wp:attachment":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}