- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Why does this code compile?
Wed, 2011-06-22, 21:15
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
// 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
Wed, 2011-06-22, 22:47
#2
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
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
Wed, 2011-06-22, 23:07
#3
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:
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
On Wed, Jun 22, 2011 at 4:14 PM, Ken McDonald <ykkenmcd@gmail.com> wrote: