Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 591

How can I simplify nested pattern matching clauses?

$
0
0

I have nested data types with a lot of option modifiers. I want to simplify the source code of functions when I need to match the nested types.

Consider example:

type ty = Ty1 | Ty2let func = function  | Some v -> begin    match v with    | Ty1 -> Printf.printf "Ty1\n"    | Ty2 -> Printf.printf "Ty2\n"  end  | None -> Printf.printf "None\n"let () =  Printf.printf "\n";  let _ =  func @@ Some(Ty1) in ()

With a large number of nested types, my code becomes very huge.

Instead, I want to write something like this:

let func = function  | Some v when (v == Ty1) -> Printf.printf "Ty1\n"  | Some v when (v == Ty2) -> Printf.printf "Ty2\n"  | None -> Printf.printf "None\n"

What is the best solution for my problem? Is it possible in OCaml syntax?

Or may be there are some design patterns that help me avoid this problem when designing user-defined data types?


Viewing all articles
Browse latest Browse all 591

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>