4 ms·
> The usual way to disambiguate in these cases is to check whether b is plausibly a type and treat it as a generic Most compilers don't even go that far: using
by seanmcdirmid 7y ago
> The usual way to disambiguate in these cases is to check whether b is plausibly a type and treat it as a generic
Most compilers don't even go that far: using type information parsing is a huge no no for almost all languages that aren't C++. So for example, Typescript will flag:
a<b>(c)
as an error if a, b, and c are untyped, meaning it automatically resolved the generic application in the parser before type information was processed, while this is fine:
a<b>=(c)
Since the equal sign means > is part of a >= token. The parser is automatically determining if generic application was meant or not, before any type info is considered.
- cwzwarich 7y agoThat's why I said "plausibly". From the TypeScript spec, it looks like it takes the Boolean grammar approach (treat it like a type expression if it's a valid type expression, otherwise don't): The grammar ambiguity is resolved as follows: In a context where one possible interpretation of a sequence of tokens is an Arguments production, if the initial sequence of tokens forms a syntactically correct TypeArguments production and is followed by a '(' token, then the sequence of tokens is processed an Arguments production, and any other possible interpretation is discarded. Otherwise, the sequence of tokens is not considered an Arguments production.
- seanmcdirmid 7y agoRight...the big weakness of this approach are the error messages, which are fairly obtuse if you ever happen to run into this corner case. Luckily, most programmers won't.