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

Re: Compiler type inference problem?

No replies
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
I agree on that, but doesn't the 
def f() {}
construct means that the last statement is irrelevant, and the result type will always be Unit? And since Unit can be only Unit...
If I had used the = I would agree with you, but I didn't...
2012/2/3 √iktor Ҡlang <viktor.klang@gmail.com>


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

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