17 people like it.

Union constructors can be used as functions

Union constructors can be used as functions

1: 
2: 
3: 
4: 
5: 
6: 
7: 
type Ast
  = Identifier of string
  | Number of int

let num f x = f x

num Number 1
union case Ast.Identifier: string -> Ast
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
union case Ast.Number: int -> Ast
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val num : f:('a -> 'b) -> x:'a -> 'b

Full name: Script.num
val f : ('a -> 'b)
val x : 'a
Raw view Test code New version

More information

Link:http://fssnip.net/31
Posted:13 years ago
Author:fholm
Tags: unions , functions