scala - how to apply implicit conversion for implicit parameter? -
i want implicitly convert connection jdbc connection implicit connection parameter in sql method parameter. have code, 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) not find implicit value parameter connection: jdbcconnection sql("hello world") ^ error:(20, 10) not enough arguments method sql: (implicit connection: jdbcconnection)unit. unspecified value parameter connection. sql("hello world")
how can fix this?
i tried use parameter implicit still compilation error
class connection(val connection: jdbcconnection) object connection { implicit def connection2jdbcconnection(implicit connection: connection) = connection.connection } error:(20, 10) not enough arguments method sql: (implicit connection: jdbcconnection)unit. unspecified value parameter connection. sql("hello world") ^ error:(20, 10) not find implicit value parameter connection: jdbcconnection sql("hello world") ^
an implicit conversion takes explicit argument used if connection
used explicitly. if want work implicitly, make connection2jdbcconnection
take implicit connection: connection
.
Comments
Post a Comment