question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ambiguous Implicit values / Diverging implicit expansion

See original GitHub issue

uPickle unable to parse through this code:

import upickle.default._
type Env = Map[String,V]
object Simple {

  type Var = String

  sealed trait Expr      //will be evaluated into V
  case class ENum(n: Double) extends Expr
  case class EVar(name: String) extends Expr
  case class EOp(op: String, es: Seq[Expr]) extends Expr
  case class EFun(formal: String, body: Expr) extends Expr
  case class EApp(fun: Expr, actual: Expr) extends Expr

  sealed trait V           //cannot be evaluated further
  case class VNum(n: Double) extends V
  case class VClosure(x: String, body: Expr, t: Map[String, V]) extends V

  sealed trait VorE     //a wrapped that excepts either Expr or V
  case class Vo(v: V) extends VorE
  case class Eo(e: Expr) extends VorE

  type Stack = List[K]

  sealed trait K     //Stack items. Exprs and variables are turned into stack items when placed on stack.
  case class KArg(e: Expr) extends K
  case class KClosure(formal: String, body: Expr, t: Map[String,V]) extends K
  case class KOp(op: String, env: Map[String,V], vals: List[V], exprs: Seq[Expr]) extends K

              // These lines compile
// def writeState(state: (List[K], Env, VorE)): String = write(state)
//   def readState(str: String): (List[K], Env, VorE) = read[(List[K], Env, VorE)](str)

           // These lines do not compile
//   def writeKClos(trial: KClosure) : String = write(trial)
//   def readKClos(str: String): KClosure = read[KClosure](str)

          // These lines do not compile.
  def write3Tuple(n: (String, Expr, Map[String, V])): String = write[(String, Expr, Map[String, V])](n)
  def read3Tuple(str: String): (String, Expr, Map[String, V]) = read[(String, Expr, Map[String, V])](str)
}

With exactly this code, I get this error message:

[info] Compiling 2 Scala sources… [error] Line 37: ambiguous implicit values: [error] both value derive$macro$249 of type => upickle.default.Writer[(String, Simple.Expr, Map[String,Simple.V])] [error] and value derive$macro$266 of type => upickle.default.Writer[Simple.EOp] [error] match expected type upickle.default.Writer[T1] [error] Error occurred in an application involving default arguments. [error] def write3Tuple(n: (String, Expr, Map[String, V])): String = write(String, Expr, Map[String, V]) [error] ^ [error] Line 38: ambiguous implicit values: [error] both value derive$macro$588 of type => upickle.default.Reader[(String, Simple.Expr, Map[String,Simple.V])] [error] and value derive$macro$605 of type => upickle.default.Reader[Simple.EOp] [error] match expected type upickle.default.Reader[T1] [error] def read3Tuple(str: String): (String, Expr, Map[String, V]) = read(String, Expr, Map[String, V]) [error] ^ [error] two errors found error Compilation failed [error] Total time: 8 s, completed Jun 15, 2016 1:44:19 PM

With the def writeKClos and def readKClos uncommented and the last two lines commented, I get:

compile [info] Compiling 2 Scala sources… [error]Line 34: diverging implicit expansion for type upickle.default.Writer[T1] [error] starting with macro method macroW in trait LowPriX [error] Error occurred in an application involving default arguments. [error] def writeKClos(trial: KClosure) : String = write(trial) [error] ^ [error]Line 35: diverging implicit expansion for type upickle.default.Reader[T1] [error] starting with macro method macroR in trait LowPriX [error] def readKClos(str: String): KClosure = readKClosure [error] ^ [error] two errors found error Compilation failed [error] Total time: 11 s, completed Jun 15, 2016 1:54:23 PM

When def writeState and def readState are not commented and all following lines are commented, the code compiles fine.

I am not sure what diverging implicit expansion means in terms of how my project is laid out. However, everything that is included in the file should compile file. It does not, and I have no idea why.

Notes: This is all done with sbt on a Mac. Nothing in the project is imported, and no other file uses this file. This is not quite what I was using in the project, but this is the simplest example where the behavior shows.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
damienfircommented, Aug 17, 2016

Yes I found a workaround. I was trying to use uPickle on a case class extending a trait and it gave me the error. Replacing the trait by a sealed trait solved the problem.

0reactions
lihaoyicommented, Dec 19, 2017

Bug bankruptcy

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ambiguous Implicit values / Diverging implicit expansion #159
I am not sure what diverging implicit expansion means in terms of how my project is laid out. However, everything that is included...
Read more >
In scala, why does the compiler doesn't explain the error ...
Existence of several suitable implicits is different error: "ambiguous implicit values". It's possible that "diverging implicit expansion" ...
Read more >
Diverging implicit expansion - Question - Scala Users
Implicit expansion is the process of finding the series of calls to implicits that is necessary for constructing a value of a certain...
Read more >
Implicits, type classes, and extension methods - kubuszok.com
Each of params will become implicit value (so you cannot get params ... get an error (ambiguous implicit or diverging implicit expansion).
Read more >
Implicit Implications (part 1): Implicit Parameters - Rally Health
The primary goal of implicit search is to find the most specific type of implicit value or method in lexographic scope to inject...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found