Description
The Dart SDK introduces new syntax in its core library by shipping a class called "Function". "Function" is not a valid type identifier and so, according to the specification, a class can't be named "Function". The conformance section in the spec explicitly disallows new syntax.
Furthermore, the analyzer parses classes with a "Function" name without syntax errors, while the ANTLR-based spec, as expected, emits an error for the following program:
class Function {}
=== Analyzer ===
Scan errors: 0
Parse errors: 0
<CompilationUnitImpl> [0-17]
┗━ <ClassDeclarationImpl> [0-17]
┣━ 'class' [0-5]
┣━ 'Function' [6-14]
┣━ '{' [15-16]
┗━ '}' [16-17]
--------------------------------------------------------------------------------
=== ANTLR ===
Errors of type 1: [[@1,6:13='Function',<94>,1:6], [@1,6:13='Function',<94>,1:6], [@2,15:15='{',<139>,1:15], [@2,15:15='{',<139>,1:15]]
Errors of type 2: []
<startSymbol>
┗━ <libraryDefinition>
┣━ <metadata>
┣━ <topLevelDefinition>
┃ ┗━ <classDeclaration>
┣━ <metadata>
┣━ <topLevelDefinition>
┃ ┗━ <classDeclaration>
┃ ┗━ 'class'
┣━ <metadata>
┣━ <topLevelDefinition>
┣━ <metadata>
┣━ <topLevelDefinition>
┃ ┣━ 'Function'
┃ ┣━ '{'
┃ ┗━ '}'
┗━ '<EOF>'
It seems to me that this "hack" exists for UX-related reasons (to e.g. allow the "Go to implementation" feature in IDEs to resolve to an actual "thing" instead of making "Function" act like a keyword-like construct).
void main() {
// ,--- "go to implementation" redirects to a real class with useful comments.
// v
Function fn;
}
I think that this is helpful, but I also think that conforming to the spec without such "hacks" would also be good.
(This "hack" seems intentional and there's probably nothing surprising here. I just couldn't find an issue that captures this exact observation.)
Cf. #45703 which seems to have dealt with disallowing users from defining custom classes called "Function" (but the SDK still ships new syntax that doesn't conform to the spec).