Matheus Moreira
Matheus Moreira

Reputation: 2388

Difficult with Scala's implicit parameter resolution

I have the following code, which is resulting in a compilation error because the compiler (2.10.3) is unable to find an implicit parameter for a method call:

package (...).construcao.light

import scala.slick.jdbc.{ GetResult, StaticQuery => Q }
import scala.slick.session.Session

import (...).rede.Rede
import (...).rede.TipoVista._
import (...).rede.construcao.SQLConnectionFactory
import (...).rede.construcao.SQLLoader
import (...).rede.construcao.SQLReader
import (...).rede.entidade.Bloco
import (...).rede.entidade.EstadoAbertura.estado

class BlocoSQLLoader(deps: {
  val fabricaConexoes: SQLConnectionFactory
  val leitorSQL: SQLReader
}) extends SQLLoader {

  import BlocoSQLLoader._

  def carregar(subestacao: String, alimentador: String, vista: TipoVista, rede: Rede) {
  }

  def pesquisarBlocos(subestacao: String, alimentador: String, vista: TipoVista) = {
    deps.fabricaConexoes.conexao(vista) withSession { implicit sessao: Session =>
      val b = Q.query[(String, String), Bloco](deps.leitorSQL("rede.blocosAlimentador"))
      b(subestacao, alimentador).list
    }
  }

}

object BlocoSQLLoader {

  import scala.language.implicitConversions

  implicit val getResultadoBloco = GetResult(r => new Bloco(
    idEquipamento = r.<<, pelFonte = r.<<, pelCarga = r.<<, idBlocoFonte = r.<<, idBlocoCarga = r.<<,
    refAlimentador = r.<<, refAlimentadorOrigem = r.<<, nome = r.<<, seccionamento = estado(r.<<),
    estado = estado(r.<<), cor = r.<<)
  )

}

The line val b = Q.query[(String, String), Bloco](deps.leitorSQL("rede.blocosAlimentador")) breaks compilation because the implicit val getResultadoBloco (on object BlocoSQLLoader) is not found. I thought that the compiler tried to find implicit parameters on a class companion object. What I'm doing wrong here?

Thanks!

Upvotes: 1

Views: 161

Answers (1)

Daniel C. Sobral
Daniel C. Sobral

Reputation: 297275

Forward implicit references must have explicit types. So either move object BlocoSQLLoader before the class declaration, or change the implicit declaration to

implicit val getResultadoBloco: WhateverThisTypeIsSupposedToBe = ...

Upvotes: 1

Related Questions