scala implicit (1)

implicit conversion and implicit value

Implicit function converts parameter to the return type implicitly if the return type is qualified but the parameter type found.

Implicit value brings the value into current scope which could be used non explicitly.

// implicit conversion from String to StringBuilder
implicit def toStringBuilder(s: String): StringBuilder = new StringBuilder(s)
val hi: String = "hi"
hi.append(" leecy") // implicit convert String to StringBuilder

// this func is actually a implicit StringBuilder value
// common to see in summonning implicit type class instance
implicit def givenStringBuilder(implicit s: String): StringBuilder = s
implicit val s: String = "hi" // implicit s defined implies StringBuilder(s) provided

def g(implicit sb: StringBuilder): String = sb.append("end").mkString

println(g) // call g directly as an implicit StringBuilder is in scope