Skip to content

Commit

Permalink
Allow type parameter defaults in function declarations
Browse files Browse the repository at this point in the history
Summary:
When type parameter defaults were added (D3196321), it was not possible to
supply explicit type arguments to function calls. Now that explicit type
arguments to function calls are supported, it can be useful to define a
polymorphic function with type parameter defaults.

Reviewed By: gabelevi

Differential Revision: D15679697

fbshipit-source-id: 309df3194a7e02f69ec398c56d004166d6e519e2
  • Loading branch information
samwgoldman authored and facebook-github-bot committed Jun 5, 2019
1 parent dc2cfac commit c18ee0d
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 219 deletions.
4 changes: 2 additions & 2 deletions src/parser/object_parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ module Object
| true, false -> None
| _ -> Some(Parse.identifier tmp_env)
) in
let tparams = Type.type_parameter_declaration_with_defaults env in
let tparams = Type.type_parameter_declaration env in
let body, extends, implements = _class env in
Ast.Statement.ClassDeclaration { Class.
id;
Expand All @@ -724,7 +724,7 @@ module Object
| T_LCURLY -> None, None
| _ ->
let id = Some (Parse.identifier env) in
let tparams = Type.type_parameter_declaration_with_defaults env in
let tparams = Type.type_parameter_declaration env in
id, tparams in
let body, extends, implements = _class env in
Ast.Expression.Class { Class.
Expand Down
8 changes: 4 additions & 4 deletions src/parser/statement_parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ module Statement
Expect.token env T_TYPE;
Eat.push_lex_mode env Lex_mode.TYPE;
let id = Type.type_identifier env in
let tparams = Type.type_parameter_declaration_with_defaults env in
let tparams = Type.type_parameter_declaration env in
Expect.token env T_ASSIGN;
let right = Type._type env in
Eat.semicolon env;
Expand Down Expand Up @@ -572,7 +572,7 @@ module Statement
Expect.token env T_TYPE;
Eat.push_lex_mode env Lex_mode.TYPE;
let id = Type.type_identifier env in
let tparams = Type.type_parameter_declaration_with_defaults env in
let tparams = Type.type_parameter_declaration env in
let supertype = match Peek.token env with
| T_COLON ->
Expect.token env T_COLON;
Expand Down Expand Up @@ -610,7 +610,7 @@ module Statement
then error env Error.UnexpectedTypeInterface;
Expect.token env T_INTERFACE;
let id = Type.type_identifier env in
let tparams = Type.type_parameter_declaration_with_defaults env in
let tparams = Type.type_parameter_declaration env in
let { Ast.Type.Interface.extends; body } = Type.interface_helper env in
Statement.Interface.({
id;
Expand Down Expand Up @@ -649,7 +649,7 @@ module Statement
let env = env |> with_strict true in
Expect.token env T_CLASS;
let id = Parse.identifier env in
let tparams = Type.type_parameter_declaration_with_defaults env in
let tparams = Type.type_parameter_declaration env in
let extends = if Expect.maybe env T_EXTENDS then Some (Type.generic env) else None in
let mixins = match Peek.token env with
| T_IDENTIFIER { raw = "mixins"; _ } -> Eat.token env; mixins env []
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
{
"errors":[
{
"loc":{"source":null,"start":{"line":1,"column":15},"end":{"line":1,"column":16}},
"message":"Unexpected token ="
},
{
"loc":{"source":null,"start":{"line":1,"column":17},"end":{"line":1,"column":23}},
"message":"Unexpected reserved type"
}
],
"type":"Program",
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":29}},
"range":[0,29],
Expand Down Expand Up @@ -44,21 +34,16 @@
"params":[
{
"type":"TypeParameter",
"loc":{"source":null,"start":{"line":1,"column":13},"end":{"line":1,"column":14}},
"range":[13,14],
"loc":{"source":null,"start":{"line":1,"column":13},"end":{"line":1,"column":23}},
"range":[13,23],
"name":"T",
"bound":null,
"variance":null,
"default":null
},
{
"type":"TypeParameter",
"loc":{"source":null,"start":{"line":1,"column":17},"end":{"line":1,"column":23}},
"range":[17,23],
"name":"string",
"bound":null,
"variance":null,
"default":null
"default":{
"type":"StringTypeAnnotation",
"loc":{"source":null,"start":{"line":1,"column":17},"end":{"line":1,"column":23}},
"range":[17,23]
}
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
{
"errors":[
{
"loc":{"source":null,"start":{"line":1,"column":23},"end":{"line":1,"column":24}},
"message":"Unexpected token ="
},
{
"loc":{"source":null,"start":{"line":1,"column":25},"end":{"line":1,"column":31}},
"message":"Unexpected reserved type"
},
{
"loc":{"source":null,"start":{"line":1,"column":35},"end":{"line":1,"column":36}},
"message":"Unexpected token {"
Expand Down Expand Up @@ -56,21 +48,16 @@
"params":[
{
"type":"TypeParameter",
"loc":{"source":null,"start":{"line":1,"column":21},"end":{"line":1,"column":22}},
"range":[21,22],
"loc":{"source":null,"start":{"line":1,"column":21},"end":{"line":1,"column":31}},
"range":[21,31],
"name":"T",
"bound":null,
"variance":null,
"default":null
},
{
"type":"TypeParameter",
"loc":{"source":null,"start":{"line":1,"column":25},"end":{"line":1,"column":31}},
"range":[25,31],
"name":"string",
"bound":null,
"variance":null,
"default":null
"default":{
"type":"StringTypeAnnotation",
"loc":{"source":null,"start":{"line":1,"column":25},"end":{"line":1,"column":31}},
"range":[25,31]
}
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
{
"errors":[
{
"loc":{"source":null,"start":{"line":1,"column":9},"end":{"line":1,"column":10}},
"message":"Unexpected token ="
},
{
"loc":{"source":null,"start":{"line":1,"column":11},"end":{"line":1,"column":17}},
"message":"Unexpected reserved type"
}
],
"type":"Program",
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"range":[0,26],
Expand Down Expand Up @@ -58,21 +48,16 @@
"params":[
{
"type":"TypeParameter",
"loc":{"source":null,"start":{"line":1,"column":7},"end":{"line":1,"column":8}},
"range":[7,8],
"loc":{"source":null,"start":{"line":1,"column":7},"end":{"line":1,"column":17}},
"range":[7,17],
"name":"T",
"bound":null,
"variance":null,
"default":null
},
{
"type":"TypeParameter",
"loc":{"source":null,"start":{"line":1,"column":11},"end":{"line":1,"column":17}},
"range":[11,17],
"name":"string",
"bound":null,
"variance":null,
"default":null
"default":{
"type":"StringTypeAnnotation",
"loc":{"source":null,"start":{"line":1,"column":11},"end":{"line":1,"column":17}},
"range":[11,17]
}
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
{
"errors":[
{
"loc":{"source":null,"start":{"line":1,"column":16},"end":{"line":1,"column":17}},
"message":"Unexpected token ="
},
{
"loc":{"source":null,"start":{"line":1,"column":18},"end":{"line":1,"column":24}},
"message":"Unexpected reserved type"
}
],
"type":"Program",
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":32}},
"range":[0,32],
Expand Down Expand Up @@ -66,21 +56,16 @@
"params":[
{
"type":"TypeParameter",
"loc":{"source":null,"start":{"line":1,"column":14},"end":{"line":1,"column":15}},
"range":[14,15],
"loc":{"source":null,"start":{"line":1,"column":14},"end":{"line":1,"column":24}},
"range":[14,24],
"name":"T",
"bound":null,
"variance":null,
"default":null
},
{
"type":"TypeParameter",
"loc":{"source":null,"start":{"line":1,"column":18},"end":{"line":1,"column":24}},
"range":[18,24],
"name":"string",
"bound":null,
"variance":null,
"default":null
"default":{
"type":"StringTypeAnnotation",
"loc":{"source":null,"start":{"line":1,"column":18},"end":{"line":1,"column":24}},
"range":[18,24]
}
}
]
}
Expand All @@ -92,8 +77,8 @@
}
]
},
"superClass":null,
"typeParameters":null,
"superClass":null,
"superTypeParameters":null,
"implements":[],
"decorators":[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
{
"errors":[
{
"loc":{"source":null,"start":{"line":1,"column":17},"end":{"line":1,"column":18}},
"message":"Unexpected token ="
},
{
"loc":{"source":null,"start":{"line":1,"column":19},"end":{"line":1,"column":25}},
"message":"Unexpected reserved type"
}
],
"type":"Program",
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":34}},
"range":[0,34],
Expand Down Expand Up @@ -70,21 +60,16 @@
"params":[
{
"type":"TypeParameter",
"loc":{"source":null,"start":{"line":1,"column":15},"end":{"line":1,"column":16}},
"range":[15,16],
"loc":{"source":null,"start":{"line":1,"column":15},"end":{"line":1,"column":25}},
"range":[15,25],
"name":"T",
"bound":null,
"variance":null,
"default":null
},
{
"type":"TypeParameter",
"loc":{"source":null,"start":{"line":1,"column":19},"end":{"line":1,"column":25}},
"range":[19,25],
"name":"string",
"bound":null,
"variance":null,
"default":null
"default":{
"type":"StringTypeAnnotation",
"loc":{"source":null,"start":{"line":1,"column":19},"end":{"line":1,"column":25}},
"range":[19,25]
}
}
]
}
Expand All @@ -96,8 +81,8 @@
}
]
},
"superClass":null,
"typeParameters":null,
"superClass":null,
"superTypeParameters":null,
"implements":[],
"decorators":[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
{
"errors":[
{
"loc":{"source":null,"start":{"line":1,"column":24},"end":{"line":1,"column":25}},
"message":"Unexpected token ="
},
{
"loc":{"source":null,"start":{"line":1,"column":26},"end":{"line":1,"column":32}},
"message":"Unexpected reserved type"
}
],
"type":"Program",
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":43}},
"range":[0,43],
Expand Down Expand Up @@ -62,21 +52,16 @@
"params":[
{
"type":"TypeParameter",
"loc":{"source":null,"start":{"line":1,"column":22},"end":{"line":1,"column":23}},
"range":[22,23],
"loc":{"source":null,"start":{"line":1,"column":22},"end":{"line":1,"column":32}},
"range":[22,32],
"name":"T",
"bound":null,
"variance":null,
"default":null
},
{
"type":"TypeParameter",
"loc":{"source":null,"start":{"line":1,"column":26},"end":{"line":1,"column":32}},
"range":[26,32],
"name":"string",
"bound":null,
"variance":null,
"default":null
"default":{
"type":"StringTypeAnnotation",
"loc":{"source":null,"start":{"line":1,"column":26},"end":{"line":1,"column":32}},
"range":[26,32]
}
}
]
}
Expand Down
Loading

0 comments on commit c18ee0d

Please sign in to comment.