From 0f1f6609b10ed0c5c7c1813e79edf90960a6b9d1 Mon Sep 17 00:00:00 2001 From: vegorov-rbx <75688451+vegorov-rbx@users.noreply.github.com> Date: Thu, 13 May 2021 19:02:51 +0300 Subject: [PATCH] RFC: Named Function Type Arguments (#36) Introduces syntax for optional names of function type arguments. --- rfcs/syntax-named-function-type-args.md | 56 +++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 rfcs/syntax-named-function-type-args.md diff --git a/rfcs/syntax-named-function-type-args.md b/rfcs/syntax-named-function-type-args.md new file mode 100644 index 0000000..fed2842 --- /dev/null +++ b/rfcs/syntax-named-function-type-args.md @@ -0,0 +1,56 @@ +# Named function type arguments + +## Summary + +Introduce syntax for optional names of function type arguments. + +## Motivation + +This feature will be useful to improve code documentation and provide additional information to LSP clients. + +## Design + +This proposal uses the same syntax that functions use to name the arguments: `(a: number, b: string) -> string` + +Names can be provided in any place where function type is used, for example: + +* in type aliases: +``` +type MyFunc = (cost: number, name: string) -> string +``` + +* in definition files for table types: +``` +declare string: { + rep: (pattern: string, repeats: number) -> string, + sub: (string, start: number, end: number?) -> string -- names are optional, here the first argument doesn't use a name +} +``` + +* for variables: +``` +local cb: (amount: number) -> number +local function foo(cb: (name: string) -> ()) +``` + +Variadic arguments cannot have a name, they are already written as ...: number. + +This feature can be found in other languages: + +* TypeScript (names are required): `let func: (p: type) => any` +* C++: `void (*f)(int cost, std::string name) = nullptr;` + +Implementation will store the names inside the function type description. + +Parsing the argument list will require a single-token lookahead that we already support. +Argument list parser will check if current token is an identifier and if the lookahead token is a colon, in which case it will consume both tokens. + +Function type comparisons will ignore the argument names, this proposal doesn't change the semantics of the language and how typechecking is performed. + +## Drawbacks + +Argument names require that we create unique function types even when these types are 'identical', so we can't compare types using pointer identity. + +This is already the case in current Luau implementation, but it might reduce the optimization opportunities in the future. + +There might also be cases of pointer identity checks that are currently hidden and named arguments might expose places where correct unification is required in the type checker.