Calling multicase active pattern directly

204 Views Asked by At

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

0

There are 0 best solutions below