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

Compiler type inference problem?

4 replies
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Dear all, 
look at the following case and why it doesn't compile:
trait Generic[T]trait Space {  def snapshot[T](item:java.lang.Object):Generic[T] } class TypeInferenceProblem {    def example(space:Space){    space.snapshot(new java.lang.Double(20d))    space.snapshot(new java.lang.Integer(30))  }
}
error: polymorphic expression cannot be instantiated to expected type;found   : [T]com.gottex.scala.types.Generic[T]required: Unit space.snapshot(new java.lang.Integer(30))

Whatever the return type of space.snapshot is, since the method will return unit, why does the compiler care?
Best
Edmondo
Hubert Plociniczak
Joined: 2009-09-12,
User offline. Last seen 42 years 45 weeks ago.
Re: Compiler type inference problem?
Looks like #4336 that was fixed recently.

hubert

On 02/03/2012 11:39 AM, Edmondo Porcu wrote:
CAM-vFRzffjk0z655zGtUZSTZ4HPbB9QFgjfCViHvYjw-aAKM5A [at] mail [dot] gmail [dot] com" type="cite"> Dear all, 
look at the following case and why it doesn't compile:
trait Generic[T] trait Space {   def snapshot[T](item:java.lang.Object):Generic[T] }  class TypeInferenceProblem {      def example(space:Space){     space.snapshot(new java.lang.Double(20d))     space.snapshot(new java.lang.Integer(30))   }
}
error: polymorphic expression cannot be instantiated to expected type; found   : [T]com.gottex.scala.types.Generic[T] required: Unit space.snapshot(new java.lang.Integer(30))

Whatever the return type of space.snapshot is, since the method will return unit, why does the compiler care?
Best
Edmondo

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Compiler type inference problem?

unit is not a subclass of everything, so this error makes perfect sense.
to fix it:

def example(space: Space) {
space.snapshot(new java.lang.Double(20d))
space.snapshot(new java.lang.Integer(30))
return
}

-------- Original-Nachricht --------
> Datum: Fri, 3 Feb 2012 11:39:12 +0100
> Von: Edmondo Porcu
> An: scala-user
> Betreff: [scala-user] Compiler type inference problem?

> Dear all,
>
> look at the following case and why it doesn't compile:
>
> trait Generic[T]
> trait Space {
> def snapshot[T](item:java.lang.Object):Generic[T]
> }
> class TypeInferenceProblem {
>
> def example(space:Space){
> space.snapshot(new java.lang.Double(20d))
> space.snapshot(new java.lang.Integer(30))
> }
>
> }
>
> error: polymorphic expression cannot be instantiated to expected type;
> found : [T]com.gottex.scala.types.Generic[T]
> required: Unit
> space.snapshot(new java.lang.Integer(30))
>
>
> Whatever the return type of space.snapshot is, since the method will
> return
> unit, why does the compiler care?
>
> Best
>
> Edmondo

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Compiler type inference problem?


On Fri, Feb 3, 2012 at 3:01 PM, Dennis Haupt <h-star@gmx.de> wrote:
unit is not a subclass of everything, so this error makes perfect sense.
to fix it:

               def example(space: Space) {
                       space.snapshot(new java.lang.Double(20d))
                       space.snapshot(new java.lang.Integer(30))
                       return

or simply: () 
               }

-------- Original-Nachricht --------
> Datum: Fri, 3 Feb 2012 11:39:12 +0100
> Von: Edmondo Porcu <edmondo.porcu@gmail.com>
> An: scala-user <scala-user@googlegroups.com>
> Betreff: [scala-user] Compiler type inference problem?

> Dear all,
>
> look at the following case and why it doesn't compile:
>
> trait Generic[T]
> trait Space {
>   def snapshot[T](item:java.lang.Object):Generic[T]
> }
> class TypeInferenceProblem {
>
>   def example(space:Space){
>     space.snapshot(new java.lang.Double(20d))
>     space.snapshot(new java.lang.Integer(30))
>   }
>
> }
>
> error: polymorphic expression cannot be instantiated to expected type;
> found   : [T]com.gottex.scala.types.Generic[T]
> required: Unit
> space.snapshot(new java.lang.Integer(30))
>
>
> Whatever the return type of space.snapshot is, since the method will
> return
> unit, why does the compiler care?
>
> Best
>
> Edmondo



--
Viktor Klang

Akka Tech LeadTypesafe - The software stack for applications that scale

Twitter: @viktorklang
Hubert Plociniczak
Joined: 2009-09-12,
User offline. Last seen 42 years 45 weeks ago.
Re: Compiler type inference problem?
Actually that one was #4853 and all the credit goes to Paul.
Anyway, fixed in trunk.

hubert

On 02/03/2012 11:55 AM, Hubert Plociniczak wrote:
4F2BBD3D [dot] 6010004 [at] epfl [dot] ch" type="cite"> Looks like #4336 that was fixed recently.

hubert

On 02/03/2012 11:39 AM, Edmondo Porcu wrote:
CAM-vFRzffjk0z655zGtUZSTZ4HPbB9QFgjfCViHvYjw-aAKM5A [at] mail [dot] gmail [dot] com" type="cite"> Dear all, 
look at the following case and why it doesn't compile:
trait Generic[T] trait Space {   def snapshot[T](item:java.lang.Object):Generic[T] }  class TypeInferenceProblem {      def example(space:Space){     space.snapshot(new java.lang.Double(20d))     space.snapshot(new java.lang.Integer(30))   }
}
error: polymorphic expression cannot be instantiated to expected type; found   : [T]com.gottex.scala.types.Generic[T] required: Unit space.snapshot(new java.lang.Integer(30))

Whatever the return type of space.snapshot is, since the method will return unit, why does the compiler care?
Best
Edmondo


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