2

I have a function foo declared in Bar object:

package test object Bar{ def foo[T : A](b: T) = println(b) def main(args: Array[String]) { foo(3) } } 

where A is declared in the same package:

sealed trait A[T] object A { implicit object AInt extends A[Int] implicit object AString extends A[String] } 

in this configuration Bar object doesn't compile and produces this error message:

could not find implicit value for evidence parameter of type test.A[Int] foo(3) ^ 

But when I place trait A and its companion object into package object test all work fine. Why?

EDIT

The problem is in order of declaration. If I place A typeclass declaration before Bar declaration everything works fine.

5
  • I think that even if you put the A object in the same package you still have to say import A._ in the main method, but I'm not sure why it works when you put it in the package object. Can you please post the working version with the package object usage? Commented Jan 15, 2014 at 12:06
  • I edited the title of this question. "disjoint type" has a very specific and very different meaning in Scala. Commented Jan 15, 2014 at 12:08
  • 1
    Order matters. Possible duplicate of this question. Commented Jan 15, 2014 at 12:21
  • 1
    Could you please provide a transcript like the following that shows how to reproduce this error? gist.github.com/retronym/8435310 Commented Jan 15, 2014 at 12:27
  • 1
    My suggestion: use implicit vals, rather than implicit objects, so that you can annotated the return type. Commented Jan 15, 2014 at 15:31

1 Answer 1

3

So the two solutions seem to be:

(1) Declare A first:

sealed trait A[T] object A { implicit object AInt extends A[Int ] implicit object AString extends A[String] } object Bar { def foo[T: A](b: T) = println(b) def main(args: Array[String]): Unit = foo(3) } 

(2) Use def or val with given return type:

object Bar { def foo[T: A](b: T) = println(b) def main(args: Array[String]): Unit = foo(3) } sealed trait A[T] object A { implicit def aInt : A[Int ] = AInt implicit def aString: A[String] = AString private object AInt extends A[Int ] private object AString extends A[String] } 

I would consider this is a compiler bug, but at least you can work around it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.