C-like languages allowing declarations in any order
The following C# code is happily accepted, and I believe the same is true
in Java, whereas a C++ compiler would barf on the equivalent because Foo
is used before being declared:
class Program
{
static void Main(string[] args)
{
Foo foo;
}
}
class Foo
{
int x;
}
How does this work in terms of syntax and parsing? Specifically, when the
compiler sees the first occurrence of Foo, how does it know to parse it as
a declaration instead of something else? Does it go by the juxtaposition
of two identifiers, or some other rule?
No comments:
Post a Comment