- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
implicit defs, return type should be declared?
Wed, 2009-01-28, 19:10
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)
}
}
Wed, 2009-01-28, 20:17
#2
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)
> }
> }
>
>
2009/1/28 Andreou Dimitris <jim.andreou@gmail.com>