标签: cake-pattern

具有覆盖抽象类型的蛋糕模式不适用于上部类型边界

我想覆盖trait中的抽象类型<:而不是=(在这里回答Scala Upper Bounds:value不是type参数的成员).

我想用蛋糕模式,但这不起作用,我不明白为什么?

trait A {
  def ping = println("ping")
}

trait Cake {
  type T
}

trait S { this: Cake =>
  type T = A
  def t: T
  t.ping
}
Run Code Online (Sandbox Code Playgroud)

好的,这个例子运行,但在我的实际用例中,我想覆盖类型<:而不是=.It似乎无法访问t函数,为什么?

trait S { this: Cake =>
  type T <: A
  def t: T
  t.ping
}
Run Code Online (Sandbox Code Playgroud)

返回错误 value ping is not a member of S.this.T

dependency-injection scala traits cake-pattern

8
推荐指数
1
解决办法
940
查看次数

蛋糕模式中显式类型自引用的有用性

似乎Scala的显式类型自引用最常见的用法是" Cake模式 ",其中模块的依赖项声明如下:

class Foo { this: A with B with C =>
  // ...
}
Run Code Online (Sandbox Code Playgroud)

通常,暂时忽略蛋糕模式A,B并且C可以引用任何类型级别的东西,例如类型参数:

class Outer[A, B, C] {
  class Inner { this: A with B with C =>
    // ...
  }
}
Run Code Online (Sandbox Code Playgroud)

......或抽象类型的成员:

class Outer {
  type A
  type B
  type C
  class Inner { this: A with B with C =>
    // ...
  }
}
Run Code Online (Sandbox Code Playgroud)

在这些情况中,我们都不能写abstract class Inner extends A with B with C …

scala cake-pattern

7
推荐指数
1
解决办法
1112
查看次数

蛋糕模式w/akka:为多个层提供隐式actor系统

我正在烘焙我的第一个蛋糕图案,所以请耐心等待.

我拿了我的工作单片应用程序,并将其切割成功能层.剪切看起来很干净,但导致两个依赖于隐式ActorSystem的层.

我尝试像这样解决这种依赖:

trait LayerA {
  this: ActorSystemProvider =>
  private implicit val implicitActorSystem = actorSystem
  import implicitActorSystem.dispatcher // implicit execution ctx
  ...
}
Run Code Online (Sandbox Code Playgroud)

......和LayerX类似

我的汇编类看起来像:

class Assembly extends LayerA with LayerB with LayerX with ActorSystemProvider
Run Code Online (Sandbox Code Playgroud)

其中ActorSystemProvider只是实例化actor系统.

这不起作用,因为ActorSystem当依赖关系被解析并且val被实例化时不存在,从而导致NPE.这看起来也很丑陋,我确信必须有一个更好/更简单的方法来处理它.

在使用蛋糕模式时,我应该如何处理图层之间的共享隐式依赖关系,就像ActorSystem在这种情况下一样?

谢谢

scala akka cake-pattern

7
推荐指数
2
解决办法
2924
查看次数

Scala Cake Pattern:将大型组件拆分为单独的文件

我想使用Cake Pattern将某些软件系统的各个部分拆分成组件,使其完全模块化,如本文所述.在最简单的情况下,我想要一些可模拟的组件,比如说Logging,Config,Database,Scripts等可能会互相使用.代码可能看起来像

trait AbstractConfig {
  def config: AbstractConfigInterface
  trait AbstractConfigInterface {
    def test: Unit
  }
}

trait SomeConfig extends AbstractConfig {
  this: Core =>  
  def config = SomeConfigImplementation
  object SomeConfigImplementation extends AbstractConfigInterface {
    def test = println("conf.test method called")
  }
}

trait AbstractDatabase {
  def database: AbstractDatabaseInterface
  trait AbstractDatabaseInterface {
    def connect: Unit
  }
}

trait SomeDatabase extends AbstractDatabase {
  this: Core =>
  def database = SomeDatabaseImplementation
  object SomeDatabaseImplementation extends AbstractDatabaseInterface {
    def connect = {
      println("connect method …
Run Code Online (Sandbox Code Playgroud)

scala cake-pattern

6
推荐指数
1
解决办法
994
查看次数

避免与Cake Pattern名称冲突

我目前正在使用Cake Pattern来实现一些优化算法.我经常遇到名字碰撞问题.例如:

trait Add[T] { this: Foo[T] =>
  def constant: T
  def plus( t1: T, t2: T ): T
  def add( t: T ) = plus( t, constant )
}

trait Mul[T] { this: Bar[T] =>
  def constant: T
  def times( t1: T, t2: T ): T
  def mul( t: T ) = times( t, constant )
}

trait Operations[T] { this: Add[T] with Mul[T] =>
  def neg( t: T ): T
}
Run Code Online (Sandbox Code Playgroud)

这里,constant在两者Add和 …

scala traits name-clash cake-pattern

6
推荐指数
1
解决办法
623
查看次数

Scaladoc无法为方法和类签名中的内部类生成链接

我有一个顶级特征,包含许多类和特征,如:

trait Trees { self: Types =>
  trait Tree
  trait IdentifiedTree extends Tree
  trait Empty extends Tree

  /** The factory for [[TypeUse]] instances */
  trait TypeUse extends Tree
  /** AST tree to represent erroneous trees */
  object BadTree extends IdentifiedTree
  /** AST tree for empty statements and trees */
  val Empty: Empty = new Empty {}
}

trait Types
Run Code Online (Sandbox Code Playgroud)

当我为它生成文档时,使用scaladoc我可以使用[[CLASS_NAME]]链接到内部类,但是scaladoc无法在签名和扩展中为树创建链接.

我使用sbt生成scaladoc,我使用以下标志:

scalacOptions in (Compile, doc) ++= Seq("-groups", "-implicits",
     "-diagrams", "-no-prefixes", "-author", "-explaintypes",
     "-language:implicitConversions,higherKinds")
Run Code Online (Sandbox Code Playgroud)

为了给你一个更好的主意,上面定义的api如下(请注意缺少的链接):

在此输入图像描述

你能告诉我我做错了吗?

scala sbt scaladoc cake-pattern

6
推荐指数
1
解决办法
424
查看次数

斯卡拉蛋糕模式的重要性

我已经开始学习scala一段时间了,现在看着蛋糕模式.我从这里得到了例子

trait UserRepositoryComponent {
  def userLocator: UserLocator

  trait UserLocator {
    def findAll: List[User]
  }
}

trait UserRepositoryJPAComponent extends UserRepositoryComponent {
  val em: EntityManager

  def userLocator = new UserLocatorJPA(em)

  class UserLocatorJPA(val em: EntityManager) extends UserLocator {
    def findAll = {
      println("Executing a JPA query")
      List(new User, new User)
    }
  }
}

trait UserServiceComponent {
  def userService: UserService

  trait UserService {
    def findAll: List[User]
  }
}

trait DefaultUserServiceComponent extends UserServiceComponent {
  this: UserRepositoryComponent =>

  def userService = new DefaultUserService

  class …
Run Code Online (Sandbox Code Playgroud)

dependency-injection scala cake-pattern

6
推荐指数
1
解决办法
377
查看次数

蛋糕模式和类型

如何def someA(in trait B)使用trait AC#MyTypein中相同B?(然后A#MyType =:= B#MyType)

trait C {
  type MyType
}


trait A {
  self: C =>
  def doSomething(s: MyType) { println(s.toString)}
}

trait B {
  self: C =>  

  def someA: A
  def myType: MyType

  def action = someA.doSomething(myType)
}

// Mix part

case class Ahoy(value: String)

trait ConcreteC extends C {  
  type MyType = Ahoy
}


class PieceOfCake extends B with ConcreteC {
  val someA = …
Run Code Online (Sandbox Code Playgroud)

types scala cake-pattern

5
推荐指数
1
解决办法
278
查看次数

传递DI使用蛋糕模式

我正在尝试使用像这样的蛋糕模式进行依赖注入:

trait FooComponent {
  val foo: Foo

  trait Foo;
}

trait AlsoNeedsFoo {
  this: FooComponent =>
}

trait RequiresFoo {
  this: FooComponent =>

  val a = new AlsoNeedsFoo with FooComponent{
    val foo: this.type#Foo = RequiresFoo.this.foo
  }

}
Run Code Online (Sandbox Code Playgroud)

但编译器抱怨RequiresFoo.this.type#Foo说它不符合预期的类型this.type#Foo.

所以问题是:是否可以在AlsoNeedsFoo内部创建一个对象,RequiresFoo以便依赖注入正常工作?

scala cake-pattern

5
推荐指数
1
解决办法
76
查看次数

蛋糕图案:特质融合

我一直在玩蛋糕模式,有些事情我还不太了解。

给出以下通用代码:

trait AServiceComponent {
  this: ARepositoryComponent =>
}

trait ARepositoryComponent {}
Run Code Online (Sandbox Code Playgroud)

以下将它们混合的方式有效

trait Controller {
  this: AServiceComponent =>
}

object Controller extends 
  Controller with 
  AServiceComponent with 
  ARepositoryComponent
Run Code Online (Sandbox Code Playgroud)

但是以下不

trait Controller extends AServiceComponent {}

object Controller extends
  Controller with
  ARepositoryComponent
Run Code Online (Sandbox Code Playgroud)

错误:

illegal inheritance; self-type Controller does not conform to AServiceComponent's selftype AServiceComponent with ARepositoryComponent
Run Code Online (Sandbox Code Playgroud)

如果我们知道依赖关系对于所有子类都是通用的,我们是否应该不能在层次结构中“推”依赖关系?

Controller只要编译器不解决不实例化就不应该允许它们具有依赖关系吗?

scala cake-pattern

5
推荐指数
1
解决办法
161
查看次数