-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Constructor syntax for discriminated unions type defs #1376
Comments
I don't think this would fly even as a proposal for the original syntax for DU definitions. What you're suggesting is inconsistent with tuple constructors and method invocation syntax - the constituent elements of |
@kerams that was a snippet from Rescript lang, however it's not hard to imagine it for F#, even with tuples would be like a regular function signature let getDistance ((x1,y1): float*float) ((x2,y2): float*float) =
(x1*x2 - y1*y2) just replace the |
Is this not #1129, more or less? |
@brianrourkeboll that one is specific to tuple syntax, this one would be to wrap in parenthesis instead of prefixing with |
All right, thanks for the clarification. However:
If #1129 were implemented, syntax like type U =
| A of (int, string) would presumably already be allowed. So I guess this proposal could be construed as an add-on to #1129 in the sense that both are trying to bring the syntax of types (and type signatures) closer to the syntax of values (expressions/patterns). |
The parenthesis/comma tuple proposal would fix the main pain point, as mentioned by @brianrourkeboll the proposed syntax on #1129 seems fairly decent |
It would, but it wouldn't mean the same thing. Just like currently, the following two are different: type U = | A of int * string
type U' = | A of (int * string) The latter allocates a tuple, whereas the former stores the two values directly in the A. That being said, I can't imagine comma syntax being implemented only for some uses of the asterisk type operator. That would be a crazy inconsistency. So if |
@Tarmil Yeah, I understand that distinction in the current syntax, but I don't think it could be the same way in #1129, since the following are currently legal: match 1, 2 with
| x : int, y -> x + y // y is a value of type int. let f (x : int, y) = x + y // y is a value of type int. I.e., at least one set of parentheses would always be required for backwards-compatibility, since in many scenarios symbols after |
I propose to allow constructor syntax on DUs without dropping existing one
Currently F# DUs use Ocaml syntax exclusively
That is inconsistent with usages elsewhere for instance both instantiation and pattern matching use constructor call syntax
Other ML languages already allow constructor syntax for union type definitions, for instance rescript, previously known as ReasonML
Pros and Cons
The constructor syntax on DUs type definition is cleaner, consistent with F# instantiation and pattern matching
Suggestion is to allow constructor syntax as alternative syntax without dropping existing one
Affidavit (please submit!)
Please tick these items by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: