Categories
Mastering Development

What is the effect of parenthesis when using tuples in discriminated union cases?

The discriminated union syntax as defined by Microsoft documentation does not include parenthesis. The following definition allows me to use field names in the type definition and is compatible with the syntax in the documentation.

type Node<'T> = L of value:'T | N of value:'T * children:Node<'T> list

This definition:

type Node<'T> = L of value:'T | N of (value:'T * children:Node<'T> list)

gives me the error:

Anonymous type variables are not permitted in this declaration

However, parenthesis are fine if I don’t use field names:

type Node<'T> = L of 'T | N of ('T * Node<'T> list)

Comments in this change request mention differences between the use of parenthesis and not but frankly, the whole discussion went over my head.

I was under the impression that I’m defining a tuple whether or not I use parenthesis, i.e. I’d expect the compiler to treat them as redundant, but there is obviously a difference.
What is it?

Leave a Reply

Your email address will not be published. Required fields are marked *