Ticket #172 (new defect)

Opened 1 year ago

Last modified 1 year ago

(Resolved) Builtins: Should the Function constructor handle type annotations and suchlike

Reported by: lth Assigned to: lth
Type: defect Priority: trivial
Milestone: Component: Spec
Version: 4 Keywords:
Cc: brendan,jeffdyer

Description

Currently the spec is silent on whether it should be possible to create new functions with the Function constructor that have type annotations, default arguments, rest arguments, and a return type annotation.

Do we want this? Here's a proposal for how to do it.

Recall that if the Function constructor is called with n parameters, the n-1 first are concatenated to form a comma-separated list of identifiers by simply joining them with "," strings.

I propose that we allow the first character of such a joined string to be a left parenthesis "(". If it is, then the entire string must be parseable as a parameter list optionally followed by a return type annotation.

Any type names in these annotations must resolve in the global scope, obviously.

For example:

new Function("(a:int, b:int):int", "a+b") new Function("(a:int" "b:int):int", "a+b")

One could implement arbitrarily complex rules here for "convenience", or one could say that the leftparen is only allowed if there are exactly two parameters passed to the constructor (the first example above would be valid but not the second).

Attachments

Change History

Changed 1 year ago by lth

  • priority changed from major to trivial
  • summary changed from Builtins: Should the Function constructor handle type annotations and suchlike to (Resolved) Builtins: Should the Function constructor handle type annotations and suchlike

From the discussion

  • => General agreement that we want to extend the Function constructor
  • => First n-1 are concatenated with commas
  • => If it starts with leftparen -- no leading spaces! -- then a return type may follow the rightparen
  • => No further rules
Note: See TracTickets for help on using tickets.