trait DB
class Connection
case class User
object JDBC
class Connection {
var lastAccess = 0L // Type inferred as Long
class Connection(val url:String) {
var lastAccess = 0L
class Connection(val url:String) {
var lastAccess = 0L
private[this] lazy val conn = JDBC.connectTo(url)
object JDBC {
def connectTo(url:String):JDBCConnection = ...
implicit val session:DBSession
case class User(first:String) {
def update(force:Boolean=false)(implicit s:DBSession) = ...
}
object JsonFeed {
def add(j:Json):JsonFeed = ...
}
object Main {
implicit def userToJson(u:User):Json = ...
implicit def newsToJson(n:News):Json = ...
JsonFeed.add(User("me")).add(News("Belighted hosts Scala"))
}
implicit class UserJson(u:User) {
def out:Out[Json] = ...
}
implicit class UserMessagePack(u:User) {
def out:Out[MessagePack] = ...
}
object JsonWorld {
import UserJson
val out = User("me").out
}
object MessagePackWorld {
import UserMessagePack
val out = User("me").out
}
trait JsonSer[U] {
def convert(u:U):Json
}
case class User(first:String)
case class News(message:String)
object Implicits {
implicit object UserJsonSer extends JsonSer[User] { //impl }
implicit object NewsJsonSer extends JsonSer[News] { //impl }
}
object JsonFeed {
def add[M : JsonSer](m:M) = implicitly[JsonSer[M]].convert(m)
}
object Main {
import Implicits._
JsonFeed.add(User("...")).add(News("..."))
}
trait Monoid[M] {
def `0`:M
def +(m1:M, m2:M)
}
object Implicits {
implicit object IntMonoid extends Monoid[Int] {
val `0` = 0
def +(i1,i2) = i1 + i2
}
implicit object ListMonoid[Int] extends Monoid[List[Int]] {
val `0` = 0
def +(l1,l2) = l1 ::: l2
}
}
object Main {
def sum[M:Monoid](list:List[M]):M = {
val m = implicitly[Monoid[M]]
list.foldLeft(m.`0`) { (curSum, i) =>
m.+(curSum, i)
}
}
sealed trait Option[A]
case class Some[A](a:A) extends Option[A]
case object None extends Option[Nothing]
trait Future[A] {
def map[B](f:A=>B)
def flatMap[B](f:A=>Future[B])
}
for {
a <- Some(1)
b <- None
} yield a + b
for {
users <- getUsers("s")
friends <- Future.sequence(
users.flatMap(friendsOf)
)
} yield friends