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

Why does this code compile?

3 replies
Ken McDonald
Joined: 2011-02-13,
User offline. Last seen 42 years 45 weeks ago.
I just realized I have the following blurb in my test cases:
// A complex is a float followed by a + or - followed by a float, followed by an "i" // The two numeric parts and the sign are named for access. val complexMatcher = Number.SignedFloat.name("re") +~ ("-"|"+").name("sign") +~ Number.SignedFloat.name("im") +~ "i" /** Match against a floating-point complex number and print the result. */ val found: Option[MatchResult] = complexMatcher.findFirstIn("3.2+4.5i") val complex = found match { case None => None case Some(mr) => mr("re") + " " + mr("sign") + " " + mr("im") + "i" } assert(complex === "3.2 + 4.5i")
The puzzling part (to me) is the "found match { ... }, which has differing result types for its two different matches. How does this manage to work?
Thanks,Ken
Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Why does this code compile?
It has the nearest common supertype, AnyRef.

On Wed, Jun 22, 2011 at 4:14 PM, Ken McDonald <ykkenmcd@gmail.com> wrote:
I just realized I have the following blurb in my test cases:
// A complex is a float followed by a + or - followed by a float, followed by an "i" // The two numeric parts and the sign are named for access. val complexMatcher = Number.SignedFloat.name("re") +~ ("-"|"+").name("sign") +~ Number.SignedFloat.name("im") +~ "i" /** Match against a floating-point complex number and print the result. */ val found: Option[MatchResult] = complexMatcher.findFirstIn("3.2+4.5i") val complex = found match { case None => None case Some(mr) => mr("re") + " " + mr("sign") + " " + mr("im") + "i" } assert(complex === "3.2 + 4.5i")
The puzzling part (to me) is the "found match { ... }, which has differing result types for its two different matches. How does this manage to work?
Thanks,Ken

Ken McDonald
Joined: 2011-02-13,
User offline. Last seen 42 years 45 weeks ago.
Re: Why does this code compile?
Hi Naftoli, thanks for the comment,
That's initially what I thought, but "val complex" seems to be set as type String, not as type Any, and I don't get what's going on there. Maybe a better question would be, if the match had failed and "found" was set to None, what type "complex" assume? Any?
Or is it the case that === can correctly compare a String and an Any that happens to be a string? Ah, that would make sense.
Thanks,Ken
Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Why does this code compile?
Static types and runtime types are two separate concepts.Static types exist only at compile time, and therefore cannot depend on what will happen at run time, such as what the value of found will be. You can get feedback on valid static types by adding various type annotations (val complex: AnyRef, val complex: String), or via command-line switches (-Xprint:typer). Runtime types exist only at run time, and do not depend on what might have happened. A reference's runtime type is unambiguously whatever class it's actually an instance of. You can find out the runtime type by calling complex.getClass.

By the way, where is === defined?

On Wed, Jun 22, 2011 at 5:45 PM, Ken McDonald <ykkenmcd@gmail.com> wrote:
Hi Naftoli, thanks for the comment,
That's initially what I thought, but "val complex" seems to be set as type String, not as type Any, and I don't get what's going on there. Maybe a better question would be, if the match had failed and "found" was set to None, what type "complex" assume? Any?
Or is it the case that === can correctly compare a String and an Any that happens to be a string? Ah, that would make sense.
Thanks,Ken

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