Questions tagged [cake-pattern]

The cake pattern is a Scala dependency injection solution that uses only native language features, without any framework support.

The cake pattern is a Scala dependency injection solution that uses only native language features, without any framework support.

References:

73 questions
6
votes
1 answer

Scaladoc fails to generate links for inner classes in method and class signatures

I have a top level trait that contains a number of classes and traits like: trait Trees { self: Types => trait Tree trait IdentifiedTree extends Tree trait Empty extends Tree /** The factory for [[TypeUse]] instances */ trait TypeUse…
Amanj Sherwany
  • 236
  • 2
  • 13
6
votes
2 answers

Avoid name collision with Cake Pattern

I'm currently currently using the Cake Pattern to implement some optimization algorithms. I often hit name collision problems. For instance: trait Add[T] { this: Foo[T] => def constant: T def plus( t1: T, t2: T ): T def add( t: T ) = plus( t,…
paradigmatic
  • 40,153
  • 18
  • 88
  • 147
6
votes
1 answer

Scala Cake Pattern: Splitting large components into separate files

I'd like to use the Cake Pattern for splitting parts of some software system into components to make it completely modular as proposed in this article. In the simplest case I'd like to have some mockable components, let's say Logging, Config,…
nab
  • 4,751
  • 4
  • 31
  • 42
5
votes
1 answer

Cake pattern: mixing in in a trait

I've been playing with the cake pattern and there's something I don't fully understand. Given the following common code: trait AServiceComponent { this: ARepositoryComponent => } trait ARepositoryComponent {} the following way of mixing them…
Artur Soler
  • 2,974
  • 2
  • 23
  • 24
5
votes
1 answer

Transitive DI using cake pattern

I'm trying to do dependency injection using the cake pattern like so: trait FooComponent { val foo: Foo trait Foo; } trait AlsoNeedsFoo { this: FooComponent => } trait RequiresFoo { this: FooComponent => val a = new AlsoNeedsFoo with…
dratewka
  • 2,104
  • 14
  • 15
5
votes
1 answer

Is it possible to integrate Cake-Pattern and Macros?

I must integrate some macros in a project which is using a cake-pattern. That pattern allowed us to avoid zillions of imports, among other advantages, so we would like to keep it. Now, we are facing a problem with some experimental macros we have…
neutropolis
  • 1,884
  • 15
  • 34
5
votes
2 answers

Cake pattern and types

How can def someA (in trait B) use trait A with the same C#MyType as in B ? (Then 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:…
jwinandy
  • 1,739
  • 12
  • 22
4
votes
1 answer

Difference between using Cake pattern and functions in Scala - why is the Cake pattern useful?

I was wondering about the difference between using functions and Cake pattern for DI in Scala. I came up with the following understanding(s), I would like to know if this understanding is correct. Let's imagine a dependency graph. 1) If we use…
jhegedus
  • 20,244
  • 16
  • 99
  • 167
4
votes
0 answers

Dependency injection using Cake Pattern vs. passing parameters in constructor

After reading this blog post I don't understand: What is the difference between using self type annotations vs. specifying constructor parameters for dependency injection ? In other words, whats the difference between this style: object Main { def…
jhegedus
  • 20,244
  • 16
  • 99
  • 167
4
votes
0 answers

Is the Cake Pattern missing from Haskell? Why and when would I need to use the Cake Pattern in Haskell?

Reading this question and this thread on Reddit inspired me to ask: Why don't people in the Haskell community seem to be missing Scala's Cake Pattern? Why is the lack (or cumbersomeness) of a Cake Pattern in Haskell not a such a big issue? The Cake…
jhegedus
  • 20,244
  • 16
  • 99
  • 167
4
votes
5 answers

In what scenario does self-type annotation provide behavior not possible with extends

I've tried to come up with a composition scenario in which self-type and extends behave differently and so far have not found one. The basic example always talks about a self-type not requiring the class/trait not having to be a sub-type of the…
Arne Claassen
  • 14,088
  • 5
  • 67
  • 106
4
votes
2 answers

Scala cake-pattern compile error with Precog config pattern

Following from this question, I have now the following: case class Pet(val name: String) trait ConfigComponent { type Config def config: Config } trait VetModule extends ConfigComponent { type Config <: VetModuleConfig def vet: Vet …
damirv
  • 125
  • 1
  • 5
4
votes
1 answer

Cake pattern: one component per implementation, or one component per trait?

I'm currently working to use the cake pattern on my application. On exemples I have found across the web the exemples are kind of basic but doesn't involve more complex needs. What I'd like to do is not so fancy: I would like to have inside a cake…
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
3
votes
1 answer

What are the benefits of the cake approach under old fashioned trait extending?

I'm trying to find out what is the difference between mixing traits via Cake pattern and mixing them via old fashioned extending. Here are my two examples: Via extending trait X { def foo() } trait Y extends X { def bar() } class Z extends Y…
Finkelson
  • 2,921
  • 4
  • 31
  • 49
3
votes
1 answer

ClassTag and path-dependent types in a cake-pattern-like flavour

I am working on a slick project and I am trying to make my database layer easily swappable between different profiles in order to write tests on an in-memory database. This question is inspired by this problem but it doesn't have anything to do with…
Aldo Stracquadanio
  • 6,167
  • 1
  • 23
  • 34