Upload
hung-lin
View
303
Download
0
Embed Size (px)
Citation preview
nescala 20132 days event in PhiladelphiaDay 1: 17 talksSpeakers from typesafe, twitter, tumblr, ...Scala book authors: , Day 2: $50 entrance fee
Bill Venners Josh Suerethunconference
Function Patterns for theAsynchronous Web
by Josh Suereth @ TypeSafe, author of Scala in Depth
The new WebAsynchronousEvent drivenData drivenBig dataSingle page designComposition of servicesUser experiences
Monadic Flowdef render(data: DbResult): Future[Html] = future(Html(...))def handleRequest(...): Unit = { for { data <- query(...) html <- render(data) } yield writeHtml(socket, html)}
Joinval users: Future[DbResult] = query(...)val projects: Future[DbResult] = query(...)val data: Future[(DbResult, DbResult)] = (users zip projects)
List[Future[DbResult]] -> Future[List[DbResult]]
The Essence of the Iterator PatternJeremy Gibbons and Bruno C. d. S. Oliveira @ Oxford University Computing Laboratory
Monadic MapApplicative FunctorsMonadic Applicative Functors...
Not only for DependencyInjection
trait UserRepositoryComponent { val userRepository: UserRepository
class UserRepository { ... }}
trait UserServiceComponent { this: UserRepositoryComponent =>
val userService: UserService
class UserService { ... }}
Traits are modulestrait UserModule { def loadUser(id: Long): User}trait TweetModule { def post(userId: Long, body: String)}trait MySQLUserModule extends UserModule { ... }trait TwitterModule extends TweetModule with UserModule { ... }val universe = new MySQLUserModule with TwitterModule {}
Package is just a namespace to avoid name collision. So,instead import, use extends/with
Explicit dependencies
Interesting problem #1
solution: use def or lazy val
trait A { val foo: String}trait B extends A { val bar = foo + "World"}class C extends B { val foo = "Hello" println(bar)}
Interesting problem #2
solution: use val
trait A { object stuff { object Foo object Bar object Baz} }trait B extends A { object moar { object Foo object Baz object Bar} }
abstract override trait TraitA { def method1(l: List[String]): List[String] } trait TraitB extends TraitA { override def method1(l: List[String]) = l :+ "traitB" }
trait TraitC extends TraitA { abstract override def method1(l: List[String]) = super.method1(l) :+ "traitC" } class ClassA { this: TraitA => def show() { println(method1(List("ClassA"))) } }