Skip to content

Instantly share code, notes, and snippets.

@harlanji
Created January 14, 2011 20:02
Show Gist options
  • Save harlanji/780141 to your computer and use it in GitHub Desktop.
Save harlanji/780141 to your computer and use it in GitHub Desktop.
Best practice for extensible SBT plugins?
harlan@plato:/tmp/scalamixin$ javap MyProject
Compiled from "mixin.scala"
public class MyProject extends BasicProject implements QUnitSupport,scala.ScalaObject{
public scala.collection.immutable.List qunitSources();
public void QUnitSupport$_setter_$qunitSources_$eq(scala.collection.immutable.List);
public void qunitRun();
public scala.collection.immutable.List jsSources();
public void JavascriptSupport$_setter_$jsSources_$eq(scala.collection.immutable.List);
public MyProject(ProjectInfo);
}
harlan@plato:/tmp/scalamixin$ javap MyProject2
Compiled from "mixin.scala"
public class MyProject2 extends BasicProject implements JavascriptSupport,QUnitSupport2,scala.ScalaObject{
public scala.collection.immutable.List qunitSources();
public void QUnitSupport2$_setter_$qunitSources_$eq(scala.collection.immutable.List);
public void qunitRun();
public scala.collection.immutable.List jsSources();
public void JavascriptSupport$_setter_$jsSources_$eq(scala.collection.immutable.List);
public MyProject2(ProjectInfo);
}
// ============= Dummy Code =============
class ProjectInfo {
}
class BasicProject(info: ProjectInfo) {
val sources = List("src/main/scala")
val resources = List("src/main/resources")
}
trait JettySupport {
def jettyRun = ()
def jettyStop = ()
}
class WebProject(info:ProjectInfo) extends BasicProject(info) with JettySupport {
val webResources = List("src/main/webapp")
val webInf = "WEB-INF/web.xml"
}
// ============= Start JS Plugin Code =============
trait JavascriptSupport {
val jsSources = List("src/main/javascript")
}
trait QUnitSupport extends BasicProject with JavascriptSupport {
val qunitSources = List("src/test/qunit")
def qunitRun = {
print("Running tests in " + qunitSources + " on JS in " + jsSources)
}
}
trait QUnitSupport2 extends BasicProject {
// Explicit Self Typed Reference
// http://www.scala-lang.org/node/124
self: JavascriptSupport =>
val qunitSources = List("src/test/qunit")
def qunitRun = {
print("Running tests in " + qunitSources + " on JS in " + jsSources)
}
}
// ============= Test Code =============
class MyProject(info:ProjectInfo) extends BasicProject(info) with QUnitSupport {
}
class MyProject2(info:ProjectInfo) extends BasicProject(info) with JavascriptSupport with QUnitSupport2 {
}
@harlanji
Copy link
Author

MyProject implicitly gets JavascriptSupport added to it, while MyProject2 needs it explicitly added. This seems to be because "with" (also "extends"?) in a trait will implicitly add that to the inheritor of the inheriting trait, while explicit self types will not add it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment