OCaml에서는 Some (result)Some result가 모두 허용됩니다. 하지만, 괄호 사용 여부는 코드의 가독성과 구문 해석 방식에 영향을 줄 수 있습니다.


📌 1. 패턴 매칭에서 Some resultSome (result) 차이

Some result (권장)

match Some 42 with
| Some result -> Printf.printf "결과: %d\\n" result
| None -> print_endline "값 없음"
;;
(* 출력: 결과: 42 *)

Some (result) (괄호를 써도 동작)

match Some 42 with
| Some (result) -> Printf.printf "결과: %d\\n" result
| None -> print_endline "값 없음"
;;
(* 출력: 결과: 42 *)

🚀 결론:


📌 2. 괄호가 필요한 경우

일반적으로 Some result처럼 쓰는 것이 더 자연스럽지만, 괄호가 반드시 필요한 경우도 있습니다.

튜플과 함께 사용할 때 괄호 필요

match Some (42, "Hello") with
| Some (num, str) -> Printf.printf "숫자: %d, 문자열: %s\\n" num str
| None -> print_endline "값 없음"
;;
(* 출력: 숫자: 42, 문자열: Hello *)

🚀 설명:

잘못된 예시 (괄호 없이 튜플 매칭)

match Some (42, "Hello") with
| Some num, str -> Printf.printf "숫자: %d, 문자열: %s\\n" num str
(* Error: This pattern matches values of type 'a option * 'b instead of 'a option *)