Why can a type been infered that can not be explicitly stated?

145 views
Skip to first unread message

Niels Hoogeveen

unread,
Apr 9, 2013, 11:27:27 AM4/9/13
to scala-l...@googlegroups.com

  trait A[C[_]]{
    def id: Int
  }
 
  object B1{
    def unapply(id: Int): Option[B1] = if(id == 1) Some(apply()) else None
  }
  case class B1() extends A[Iterable]{
    val id = 1
  }
  object B2{
    def unapply(id: Int): Option[B2] = if(id == 2) Some(apply()) else None
  }
  case class B2() extends A[Option]{
    val id = 2
  }

  def pf(id: Int) = id match{
    case B1(a) => a
    case B2(a) => a
  }
According to REPL the return type of pf is: Product with Serializable with A[_ >: Iterable with Option <: Equals]
 

However when explicity typing pf with that signature leads to an error condition:

Multiple markers at this line
 - class Option takes type parameters
 - _$1 takes no type parameters,
   expected: one
 - type Iterable takes type parameters

 

Aleksey Nikiforov

unread,
Apr 9, 2013, 11:37:46 AM4/9/13
to scala-l...@googlegroups.com
The full types can be long and very verbose, so they are simplified into human readable form when reporting errors or warnings. You can try "-explaintypes" switch, but there are no requirements for scalac to report types that can be readily compiled when copy-pasted.


--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-languag...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Aleksey Nikiforov

unread,
Apr 9, 2013, 12:09:04 PM4/9/13
to scala-l...@googlegroups.com
I forgot to mention that REPL type printer probably uses the same type printer as compiler.


Having said that, I could not come up with a way to express your type, because of the higher-kinded wildcard type. Best I could come up with is this:

Product with Serializable with A[T] forSome { type T[E] >: Iterable[E] with Option[E] <: Equals }

Which compiles as a type, but does not match the actual return type of pf(id: Int) function. This one is a puzzler.

Niels Hoogeveen

unread,
Apr 9, 2013, 1:43:00 PM4/9/13
to scala-l...@googlegroups.com
Thank you Aleksey... I would really like to learn how to express types like these, even though I realize they are ugly beasts.

Paul Phillips

unread,
Apr 9, 2013, 6:23:18 PM4/9/13
to scala-l...@googlegroups.com

On Tue, Apr 9, 2013 at 8:27 AM, Niels Hoogeveen <nielsh...@gmail.com> wrote:
According to REPL the return type of pf is: Product with Serializable with A[_ >: Iterable with Option <: Equals]

That's not a real type. The compiler gets confused pretty easily when it gets down to lubbing type constructors.

If the compiler could hang onto its marbles a little better it would write that type something like this:

   Product with Serializable with (A[CC] forSome { type CC[X] >: Iterable[X] with Option[X] <: Equals })
Reply all
Reply to author
Forward
0 new messages