From 0e76ee7b9b8be0a647d3f28668df640c1ea8dcb9 Mon Sep 17 00:00:00 2001
From: Vyacheslav Egorov <vegorov@roblox.com>
Date: Wed, 5 May 2021 16:26:03 +0300
Subject: [PATCH] RFC: Named function type arguments

---
 rfcs/syntax-named-function-type-args.md | 51 +++++++++++++++++++++++++
 1 file changed, 51 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 00000000..8c09f281
--- /dev/null
+++ b/rfcs/syntax-named-function-type-args.md
@@ -0,0 +1,51 @@
+# 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.
+
+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.