- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Compiler type inference problem?
Fri, 2012-02-03, 11:39
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
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
Fri, 2012-02-03, 15:11
#2
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
Fri, 2012-02-03, 15:21
#3
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
Fri, 2012-02-03, 16:01
#4
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:
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
hubert
On 02/03/2012 11:39 AM, Edmondo Porcu wrote: