This page is no longer maintained — Please continue to the home page at www.scala-lang.org

implicit defs, return type should be declared?

2 replies
ounos
Joined: 2008-12-29,
User offline. Last seen 3 years 44 weeks ago.

Hi,

I made a simple implicit method "allEqual". If I don't explicitly
declare the return type, list.allEqual call fails.
I.e. this works:

implicit def allEqual[T](x: Iterable[T]): AllEqual[T] = new AllEqual[T](x)

This doesn't:
implicit def allEqual[T](x: Iterable[T]) = new AllEqual[T](x)

Why doesn't the second work? The return type should obviously be
AllEqual[T], no?

full test code:

object Test {
class AllEqual[T](delegate: Iterable[T]) {
def allEqual = {
val first = delegate.elements.next
delegate.forall(_ == first)
}
}
implicit def allEqual[T](x: Iterable[T]): AllEqual[T] = new
AllEqual[T](x)

def main(args: Array[String]) = {
val list = List(1, 3, 5, 10)

println(list.allEqual)
}
}

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: implicit defs, return type should be declared?
Both versions work fine on the latest trunk.  What Scala version are you using?

2009/1/28 Andreou Dimitris <jim.andreou@gmail.com>
Hi,

I made a simple implicit method "allEqual". If I don't explicitly declare the return type, list.allEqual call fails.
I.e. this works:

implicit def allEqual[T](x: Iterable[T]): AllEqual[T] = new AllEqual[T](x)

This doesn't:
implicit def allEqual[T](x: Iterable[T]) = new AllEqual[T](x)

Why doesn't the second work? The return type should obviously be AllEqual[T], no?

full test code:

object Test {
  class AllEqual[T](delegate: Iterable[T]) {
      def allEqual = {
          val first = delegate.elements.next
          delegate.forall(_ == first)
      }
  }
  implicit def allEqual[T](x: Iterable[T]): AllEqual[T] = new AllEqual[T](x)

  def main(args: Array[String]) = {
      val list = List(1, 3, 5, 10)

      println(list.allEqual)
  }
}

ounos
Joined: 2008-12-29,
User offline. Last seen 3 years 44 weeks ago.
Re: implicit defs, return type should be declared?

Thanks. I'm using 2.7.2, so ok, I will upgrade :)

O/H Ricky Clarkson έγραψε:
> Both versions work fine on the latest trunk. What Scala version are
> you using?
>
> 2009/1/28 Andreou Dimitris >
>
> Hi,
>
> I made a simple implicit method "allEqual". If I don't explicitly
> declare the return type, list.allEqual call fails.
> I.e. this works:
>
> implicit def allEqual[T](x: Iterable[T]): AllEqual[T] = new
> AllEqual[T](x)
>
> This doesn't:
> implicit def allEqual[T](x: Iterable[T]) = new AllEqual[T](x)
>
> Why doesn't the second work? The return type should obviously be
> AllEqual[T], no?
>
> full test code:
>
> object Test {
> class AllEqual[T](delegate: Iterable[T]) {
> def allEqual = {
> val first = delegate.elements.next
> delegate.forall(_ == first)
> }
> }
> implicit def allEqual[T](x: Iterable[T]): AllEqual[T] = new
> AllEqual[T](x)
>
> def main(args: Array[String]) = {
> val list = List(1, 3, 5, 10)
>
> println(list.allEqual)
> }
> }
>
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland