Skip to content

Instantly share code, notes, and snippets.

@tysonjh
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tysonjh/9438697 to your computer and use it in GitHub Desktop.
Save tysonjh/9438697 to your computer and use it in GitHub Desktop.
delayedInt experimentation
/*
scala version := 2.10.3
After running into some problems with the App trait which extends the DelayedInt trait,
I've done some testing to see what is happening. There were some interesting subtleties
that I previously overlooked.
KEEP IN MIND THIS: https://issues.scala-lang.org/browse/SI-4680
- Empty constructors cause delayedInit to not be invoked (some examples illustrate this)
*/
trait Stage1 extends DelayedInit {
println("Stage1 constructor")
override def delayedInit(x: => Unit) {
println("Stage1 delayedInit before x")
x
println("Stage1 delayedInit after x")
}
}
trait Stage2 {
println("Stage2 constructor")
}
trait Stage3 {
println("Stage3 constructor")
}
trait Stage123 extends Stage1 with Stage2 with Stage3 {
println("Stage123 constructor")
}
trait Stage1Di23 extends Stage1 {
this: Stage2 with Stage3 =>
println("Stage1Di23 constructor")
}
trait Stage12Di3 extends Stage1 with Stage2 {
this: Stage3 =>
println("Stage12Di3 constructor")
}
/*
Stage1 constructor
Stage2 constructor
Stage3 constructor
Stage123 constructor
Main application constructor end
*/
object Main123 extends App {
println("Main application constructor start")
new Stage123 {}
println("Main application constructor end")
}
/*
Main application constructor start
Stage1 constructor
Stage2 constructor
Stage3 constructor
Stage123 constructor
Stage1 delayedInit before x
Stage123 NonEmpty constructor
Stage1 delayedInit after x
Main application constructor end
*/
object Main123NonEmpty extends App {
println("Main application constructor start")
new Stage123 {
println("Stage123 NonEmpty constructor")
}
println("Main application constructor end")
}
/*
Main application constructor start
Stage1 constructor
Stage1Di23 constructor
Stage2 constructor
Stage3 constructor
Main application constructor end
*/
object Main1Di23 extends App {
println("Main application constructor start")
new Stage1Di23 with Stage2 with Stage3 {}
println("Main application constructor end")
}
/*
Main application constructor start
Stage1 constructor
Stage1Di23 constructor
Stage2 constructor
Stage3 constructor
Stage1 delayedInit before x
Stage1Di23 NonEmpty constructor
Stage1 delayedInit after x
Main application constructor end
*/
object Main1Di23NonEmpty extends App {
println("Main application constructor start")
new Stage1Di23 with Stage2 with Stage3 {
println("Stage1Di23 NonEmpty constructor")
}
println("Main application constructor end")
}
/*
Main application constructor start
Stage1 constructor
Stage2 constructor
Stage12Di3 constructor
Stage3 constructor
Stage1 delayedInit before x
Stage12Di3 NonEmpty constructor
Stage1 delayedInit after x
Main application constructor end
*/
object Main12Di3NonEmpty extends App {
println("Main application constructor start")
new Stage12Di3 with Stage3 {
println("Stage12Di3 NonEmpty constructor")
}
println("Main application constructor end")
}
trait StageCakeComponent {
val stage: StageCake
trait StageCake {
def printStage() {
println("StageComponent Stage constructor")
}
}
}
trait Stage1CakeComponent extends StageCakeComponent {
override val stage: Stage1Cake
trait Stage1Cake extends StageCake {
override def printStage() {
println("Stage1 constructor")
}
}
}
trait Stage2CakeComponent extends StageCakeComponent {
override val stage: Stage2Cake
trait Stage2Cake extends StageCake {
override def printStage() {
println("Stage2 constructor")
}
}
}
trait CakeDiStage1 {
this: StageCakeComponent with Stage1 =>
println("CakeDiStage1 constructor")
}
trait CakeDiStage12 {
this: StageCakeComponent with Stage1 with Stage2 =>
println("CakeDiStage12 constructor")
}
trait Stage1CakeDiStage2 extends Stage1 {
this: StageCakeComponent with Stage2 =>
println("CakeStage1DiStage2 constructor")
}
/*
Main application constructor start
CakeDiStage1 constructor
Stage1 constructor
Stage1 delayedInit before x
Stage1Cake NonEmpty constructor
Stage1 delayedInit after x
Main application constructor end
*/
object MainCakeDi1With1WithComp1 extends App {
println("Main application constructor start")
new CakeDiStage1 with Stage1 with Stage1CakeComponent {
override val stage = new Stage1Cake {
println("Stage1Cake NonEmpty constructor")
}
}
println("Main application constructor end")
}
/*
Main application constructor start
CakeDiStage1 constructor
Stage1 constructor
Stage2 constructor
Stage1 delayedInit before x
Stage2Cake NonEmpty constructor
Stage1 delayedInit after x
Main application constructor end
*/
object MainCakeDi1With1With2WithComp2 extends App {
println("Main application constructor start")
new CakeDiStage1 with Stage1 with Stage2 with Stage2CakeComponent {
override val stage = new Stage2Cake {
println("Stage2Cake NonEmpty constructor")
}
}
println("Main application constructor end")
}
/*
Main application constructor start
CakeDiStage1 constructor
Stage2 constructor
Stage1 constructor
Stage1 delayedInit before x
Stage2Cake NonEmpty constructor
Stage1 delayedInit after x
Main application constructor end
*/
object MainCakeDi1With2With1WithComp2 extends App {
println("Main application constructor start")
new CakeDiStage1 with Stage2 with Stage1 with Stage2CakeComponent {
override val stage = new Stage2Cake {
println("Stage2Cake NonEmpty constructor")
}
}
println("Main application constructor end")
}
/*
Main application constructor start
CakeDiStage12 constructor
Stage1 constructor
Stage2 constructor
Stage1 delayedInit before x
Stage1Cake NonEmpty constructor
Stage1 delayedInit after x
Main application constructor end
*/
object MainCakeDi12With1With2WithComp1 extends App {
println("Main application constructor start")
new CakeDiStage12 with Stage1 with Stage2 with Stage1CakeComponent {
override val stage = new Stage1Cake {
println("Stage1Cake NonEmpty constructor")
}
}
println("Main application constructor end")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment