I want to implicitly convert Connection to JDBC Connection for implicit connection parameter in SQL method parameter. I have this code, which throw compilation error.
class JDBCConnection
class Connection(val connection: JDBCConnection)
object Connection {
implicit def Connection2JDBCConnection(connection: Connection) = connection.connection
}
object DB {
def withTransaction[A](block: (Connection => A)) = block(new Connection(new JDBCConnection))
}
object Main {
def SQL(query: String)(implicit connection: JDBCConnection) = println("SQL:" + query)
def main(args: Array[String]) = {
DB.withTransaction { implicit connection =>
SQL("Hello world")
}
}
}
Error:(20, 10) could not find implicit value for parameter connection: JDBCConnection
SQL("Hello world")
^
Error:(20, 10) not enough arguments for method SQL: (implicit connection: JDBCConnection)Unit.
Unspecified value parameter connection.
SQL("Hello world")
How can I fix this?
I tried to use a parameter as an implicit but still get a compilation error
class Connection(val connection: JDBCConnection)
object Connection {
implicit def Connection2JDBCConnection(implicit connection: Connection) = connection.connection
}
Error:(20, 10) not enough arguments for method SQL: (implicit connection: JDBCConnection)Unit.
Unspecified value parameter connection.
SQL("Hello world")
^
Error:(20, 10) could not find implicit value for parameter connection: JDBCConnection
SQL("Hello world")
^