如何创建不需要显式构建方法的类型安全构建器?

时间:2015-07-14 06:52:58

标签: scala builder

我使用了轻微滥用构建器模式来制作流畅的命令式执行链。我所追求的是一种使编译错误成为最后忘记执行方法的方法。我的目标是以下

WithServiceA {
 doStuff()
} WithServiceB {
  doStuff()
} withClient client

WithServiceAWithServiceB都可以返回值,所以如果使用返回值,很明显返回类型是错误的,但如果它们被强制使用,整个对象就会落在地板默默地。我想确保忘记withClient调用是编译错误,无论它在何种上下文中使用。

我希望能够跳过块,如果它们是不需要的并按任意顺序放置,所以我希望替换之前使用的嵌套内部类模式ala

def onServiceA[A](body: ServiceA => A) = new {   
  def onServiceB[B >: A](body: ServiceB => B) = {b => {
    doStuff()
  }
}

1 个答案:

答案 0 :(得分:5)

它看起来像是类型安全的构建器模式。请参阅this answer

在你的情况下:

trait TTrue
trait TFalse

class Myclass[TA, TB, TC] private(){
  def withServiceA(x: => Unit)(implicit e: TA =:= TFalse) = {x; new Myclass[TTrue, TB, TC]}
  def withServiceB(x: => Unit)(implicit e: TB =:= TFalse) = {x; new Myclass[TA, TTrue, TC]}
  def withServiceC(x: => Unit)(implicit e: TC =:= TFalse) = {x; new Myclass[TA, TB, TTrue]}
  def withClient(x: => Unit)(implicit e1: TA =:= TTrue, e2: TB =:= TTrue) = x
}

object Myclass{
  def apply() = new Myclass[TFalse, TFalse, TFalse]
}

用法:

Myclass()
  .withClient(println("withClient"))
//<console>:22: error: Cannot prove that TFalse =:= TTrue.
//                .withClient(println("withClient"))
//                           ^


Myclass()
  .withServiceB(println("with B"))
  .withServiceA(println("with A"))
  .withClient(println("withClient"))
//with B
//with A
//withClient

Myclass()
  .withServiceA(println("with A"))
  .withServiceC(println("with C"))
  .withServiceB(println("with B"))
  .withClient(println("withClient"))
//with A
//with C
//with B
//withClient

Myclass()
  .withServiceC(println("with C"))
  .withServiceB(println("with B"))
  .withServiceA(println("with A"))
  .withServiceC(println("with C2"))
  .withClient(println("withClient"))
//<console>:25: error: Cannot prove that TTrue =:= TFalse.
//                .withServiceC(println("with C2"))
//                             ^

您可以提供自定义错误消息,其中包含=:=类的自定义替换。

如果您想确保在调用每个Myclass.apply withClient之后,您可以像这样手动调用它:

sealed class Context private()
object Context {
   def withContext(f: Context => Myclass[TTrue, TTrue, _])(withClient: => Unit) =
     f(new Context).withClient(withClient)
}

object Myclass{
  def apply(c: Context) = new Myclass[TFalse, TFalse, TFalse]
}

用法:

Context
  .withContext(
    Myclass(_)
      .withServiceA(println("with A"))
      .withServiceC(println("with C"))
      .withServiceB(println("with B"))
  )(println("withClient"))

On ideone

无法在Myclass方法之外创建withContextwithClient将至少调用一次。