why does "type inference" work differently when moving the statements into a local curly braces?

53 views
Skip to first unread message

woshilaiceshide

unread,
Mar 30, 2016, 10:21:55 AM3/30/16
to scala-user
Hi, I'm caught by a strange problem:
why does "type inference" work differently when moving the statements into a local curly braces?

See the following example please:
//the good codes
Object Wrapper{
  case class Foo[A](f: A => A)
  def fail(foo: Any, x: Any) = foo match { case Foo(f) => f(x) }
}


//the bad codes, which is just different from the above slightly: only move the body
Object Wrapper{
  {
    case class Foo[A](f: A => A)
    //error: type mismatch; found : x.type (with underlying type Any) required: A where type +A
    def fail(foo: Any, x: Any) = foo match { case Foo(f) => f(x) }
  }
}


SO STRANGE!
What I see is just that "fail" is a member of Wrapper in the good one, while it's a local method in the bad one.

What's the matter? Any help?
Many thanks.


Bjorn Regnell

unread,
Mar 30, 2016, 1:50:04 PM3/30/16
to scala-user
When pasting your code (with object decapitalized) into the REPL it compiles on my box, as can be seen below.
Can you replicate your error message with the code below? Which version of Scala are you running?

Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions for evaluation. Or try :help.

scala> :pa
// Entering paste mode (ctrl-D to finish)

object Wrapper1{

  case class Foo[A](f: A => A)
  def fail(foo: Any, x: Any) = foo match { case Foo(f) => f(x) }
}

object Wrapper2{

  {
    case class Foo[A](f: A => A)
    def fail(foo: Any, x: Any) = foo match { case Foo(f) => f(x) }
  }
}

// Exiting paste mode, now interpreting.

defined object Wrapper1
defined object Wrapper2

scala> Wrapper1.fail(42,42)
scala.MatchError: 42 (of class java.lang.Integer)
  at Wrapper1$.fail(<console>:15)
  ... 32 elided

scala> Wrapper2.fail(42,42)
<console>:15: error: value fail is not a member of object Wrapper2
       Wrapper2.fail(42,42)
                ^

scala>

Stephen Compall

unread,
Mar 30, 2016, 2:07:49 PM3/30/16
to scala...@googlegroups.com
On 2016-03-30 7:21 AM, woshilaiceshide wrote:
> What I see is just that "fail" is a member of Wrapper in the good one,
> while it's a local method in the bad one.

Yes, that's right. The scope of the definition is determined by its
location in the syntax.

There's nothing wrong here; this is how it works.

--
Stephen Compall
If anyone in the MSA is online, you should watch this flythrough.

som-snytt

unread,
Mar 30, 2016, 7:20:19 PM3/30/16
to scala-user

Fixed in 2.11.8. I guess you always need to say which version shows the problem.

There have been issues with local objects.

Following http://www.scala-lang.org/news/2.11.8/

https://issues.scala-lang.org/browse/SI-9567

https://github.com/scala/scala/pull/4862

in case you want to check if that's the fix. It seems to have something to do with looking up the case class.

woshilaiceshide

unread,
Mar 31, 2016, 1:34:23 AM3/31/16
to scala-user
please use the "scalac" command directly(not the repl, or the "scala" command), and you'll see the error.
BTW, I've forgotten to write version the target version of scala. as @som-snytt said in this thread, this is a bug in scala-2.11.7, but is fixed in scala-2.11.8.

woshilaiceshide

unread,
Mar 31, 2016, 1:36:40 AM3/31/16
to scala-user
Thanks for your answer. It's just a bug and fixed in scala-2.11.8.

I'll write down the target version of scala next time.
Reply all
Reply to author
Forward
0 new messages