The difference between discriminatory types of Union in F #

I read about F # and look at the source code of people, and sometimes I see

Type test =
   | typeone
   | typetwo 

And sometimes I see

type test = typeone | typetwo 

One of them has a pipe before, and the other does not. At first I thought one of them was renamed against the discriminated Union, but I THINK that they are the same. Can someone explain the difference, if any?

+4
source share
3 answers

There is no difference. These notation is completely equivalent. The lead channel symbol is optional.

. , , , . , :

type Large =
  | Case1 of int * string
  | Case2 of bool
  | SomeOtherCase
  | FinalCase of SomeOtherType

, - , . , :

type QuickNSmall = One | Two | Three
+7

.

| .

:

union-type-cases: = '|' opt union-type-case '|'... '|'

,

Type test =
   | typeone = 1
   | typetwo = 2
+5

As already mentioned, a facilitator |is optional.

The examples in the other answers do not show this, so it’s worth adding that you can omit it even for a multi-line discriminatory union (and include it when defining a single line union):

type Large =
    Case1 of int * string
  | Case2 of bool
  | SomeOtherCase
  | FinalCase of SomeOtherType

type QuickNSmall = | One | Two | Three

I think most people just find these ugly (including me!) And therefore they are usually written as you see in the other answers.

+2
source

All Articles