Function call or type cast?
One example of an ambiguous statement in a C-style language that supports type names and object names overlapping is if there is a function or function pointer called x
and a type called x
.
The expression (x)(10)
would be ambiguous because there is no way of knowing just by looking if I am calling the x
object with 10
as an argument, or if I am casting 10
to type x
.
One way to disambiguate is to use an extra set of parenthesis around the x
like ((x))(10)
. This would unambiguously refer to the function call because as there is no expression after (x)
in this case so there is no cast.
Types cannot be 'parenthesized' by themselves by putting parenthesis around an ambiguous name so we are forcing the compiler to treat the name as an expression.
As such, in ambiguous cases, the type name can be the 'default.' And to opt into the expression version, we can place parenthesis around the type.