1

Consider a single case active pattern :

let (|ToUpper|) (input : string) = input.ToUpper();;

I can call the above single case active pattern outside an explicit match:

let g ( ToUpper  x ) =x ;;
> val g : string -> string

g ("hello");;              
> val it : string = "HELLO"

If I have the following multicase active pattern :

let (|Even|Odd|) n =  if (n % 2 = 0) then Even else Odd ;;

How do I call the above multicase active pattern directly ? The obvious way does not work :

let h ( |Even|Odd| n) = n;;
let h ( |Even|Odd| n) = n;;  
-------------------^
/home/shing/stdin(76,20): error FS0623: Active pattern case identifiers must begin with an uppercase letter

The reason for the above request is to write a test on the active pattern, without introducing a match explicitly. At present, if I want to test the active pattern |Even|Odd|, I need to introduced the following isEven function.

let isEven n =
    match n with
    | Even -> true
    | Odd -> false


[<Test>]
let evenTest1()  =
    (isEven 6) |> should equal true

It would be nice if I could do :

[<Test>]
let evenTest1()  =
    (|Even|Odd| 6 ) |> should equal Even   // Does not work. 

Thanks in advance for any assistance!

Shing

shing
  • 75
  • 1
  • 5
  • Why would you want to? If you want to call directly, write a function. If you want both, wrap your active pattern with a function. – Mau Aug 01 '14 at 15:59
  • 3
    `let x = (|Even|Odd|) 6` – Matthew Mcveigh Aug 01 '14 at 16:01
  • 4
    Note that you're not actually calling it "directly" with `g (ToUpper x) = x`, you're asking it to match something with a single case match. It's exactly the same as using discriminated union cases and tuples patterns in a definition, e.g. `g (x, y) = x`. – Ganesh Sittampalam Aug 01 '14 at 17:21

0 Answers0