Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pattern match multiple dates

According to this answer, I can match against DateTime.MinValue like this:

let result = 
  match date with 
  | d when d = DateTime.MinValue -> 1
  | _                            -> 0

How do I do this, if I have a match like this?

let result = 
  match (startDate, endDate) with 

This doesn’t work:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let result = 
  match (startDate, endDate) with 
  | d when d = DateTime.MinValue, e when e = DateTime.MinValue  -> 0

Compiler error for the second when:

Unexpected keyword 'when' in pattern matching. Expected '->' or other token.

>Solution :

when can be added to a whole pattern, not to nested patterns, so you need something like this:

match (startDate, endDate) with
| d, e when d = DateTime.MinValue && e = DateTime.MinValue -> 0
| _ -> 1

Note that in this case, pattern matching is not really necessary, and you can go for a simpler if:

if d = DateTime.MinValue && e = DateTime.MinValue then
    0
else
    1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading