fork download
  1. object Main extends App {
  2. trait TTrue
  3. trait TFalse
  4.  
  5. class Myclass[TA, TB, TC] private(){
  6. def withServiceA(x: => Unit)(implicit e: TA =:= TFalse) = {x; new Myclass[TTrue, TB, TC]}
  7. def withServiceB(x: => Unit)(implicit e: TB =:= TFalse) = {x; new Myclass[TA, TTrue, TC]}
  8. def withServiceC(x: => Unit)(implicit e: TC =:= TFalse) = {x; new Myclass[TA, TB, TTrue]}
  9. def withClient(x: => Unit)(implicit e1: TA =:= TTrue, e2: TB =:= TTrue) = x
  10. }
  11.  
  12. object Myclass{
  13. def apply(c: Context) = new Myclass[TFalse, TFalse, TFalse]
  14. }
  15.  
  16. sealed class Context private()
  17. object Context {
  18. def withContext(f: Context => Myclass[TTrue, TTrue, _])(withClient: => Unit) =
  19. f(new Context).withClient(withClient)
  20. }
  21.  
  22. Context
  23. .withContext(
  24. Myclass(_)
  25. .withServiceA(println("with A"))
  26. .withServiceC(println("with C"))
  27. .withServiceB(println("with B"))
  28. )(println("withClient"))
  29. }
Success #stdin #stdout 0.39s 322176KB
stdin
Standard input is empty
stdout
with A
with C
with B
withClient