- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
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
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










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
Re: Compiler type inference problem?
On Fri, Feb 3, 2012 at 3:01 PM, Dennis Haupt <h-star [at] gmx [dot] de> wrote:
or simply: ()
--
Viktor Klang
Akka Tech LeadTypesafe - The software stack for applications that scale
Twitter: @viktorklang
Re: Compiler type inference problem?
hubert
On 02/03/2012 11:39 AM, Edmondo Porcu wrote:
Re: Compiler type inference problem?
Anyway, fixed in trunk.
hubert
On 02/03/2012 11:55 AM, Hubert Plociniczak wrote: