Thursday, 16 August 2018

Cake pattern

(don't read this post if you are eating because it might make you throw up)

trait DataReader {
def readName(): String
}
trait DummyDataReader extends DataReader {
def readName(): String = "bEllA"
}
class GreeterService {
self: DataReader =>
def greet(): Unit = {
val formattedName = self.readName().toLowerCase.capitalize
println("Hi there, " + formattedName)
}
}
object Main extends App {
val greeter = new GreeterService() with DummyDataReader
greeter.greet()
// if you want to know why people hate the cake pattern then here is the answer:
greeter.readName()
// wtf?! It exposes all the methods of all the dependencies! Grim
}
view raw Cake.scala hosted with ❤ by GitHub

No comments:

Post a Comment

Scala with Cats: Answers to revision questions

I'm studying the 'Scala with Cats' book. I want the information to stick so I am applying a technique from 'Ultralearning...