implicit conversion not matching as expected

View: New views
3 Messages — Rating Filter:   Alert me  

implicit conversion not matching as expected

by J Robert Ray :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Given:

   implicit def option2array[T](t : Option[T]) : Array[T] = t match {
      case Some(x) =>
          val a = new Array[T](1)
          a(0) = x
          a

      case None =>
          new Array[T](0)
  }


This doesn't work implicitly:

  def f : Array[Int] = Some(5)

error: no type parameters for method Some: (A)Some[A] exist so that it
can be applied to arguments (Int)
   --- because ---
result type Some[A] is incompatible with expected type Array[Int]


But does work explicitly:

  def f : Array[Int] = option2array(Some(5))


A colleague discovered these also work:

   def f : Array[Int] = Some[Int](5)

   def f : Array[Int] = { val z = Some(5) ; z }


Is this working as intended?


Re: implicit conversion not matching as expected

by Eric Willigers :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

J Robert Ray wrote:

> Given:
>
>   implicit def option2array[T](t : Option[T]) : Array[T] = t match {
>      case Some(x) =>
>          val a = new Array[T](1)
>          a(0) = x
>          a
>
>      case None =>
>          new Array[T](0)
>  }
>
>
> This doesn't work implicitly:
>
>  def f : Array[Int] = Some(5)
>
> error: no type parameters for method Some: (A)Some[A] exist so that it
> can be applied to arguments (Int)
>   --- because ---
> result type Some[A] is incompatible with expected type Array[Int]

This is a bug. I tried an old Scala compiler (2.6.0-RC2) and it worked,
then tried 2.6.1-final and received the same message as yourself.


Re: Re: implicit conversion not matching as expected

by J Robert Ray :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Eric Willigers wrote:
>
> This is a bug. I tried an old Scala compiler (2.6.0-RC2) and it worked,
> then tried 2.6.1-final and received the same message as yourself.
>
>

Okay, thanks.  I submitted a bug report.

https://lampsvn.epfl.ch/trac/scala/ticket/347