简体   繁体   中英

F# pattern matching

I'm puzzled by how pattern matching works in F# for let . I'm using the Visual Studio 'F# interactive' window, F# version 1.9.7.8. Say we define a simple type:

type Point = Point of int * int ;;

and the try to pattern match against values of Point using let .

let Point(x, y) = Point(1, 2) in x ;;

fails with error FS0039: The value or constructor 'x' is not defined . How is one supposed to use pattern matching with let ?

The most curious thing is that:

let Point(x, y) as z = Point(1, 2) in x ;;

returns 1 as expected. Why?

You need to put parenthesis around your pattern:

let (Point(x, y)) = Point(1, 2) in x ;;

Otherwise there's no way to distinguish the pattern from a function-binding...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM