OCaml에서는 Some (result)와 Some result가 모두 허용됩니다. 하지만, 괄호 사용 여부는 코드의 가독성과 구문 해석 방식에 영향을 줄 수 있습니다.
Some result와 Some (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 *)
🚀 결론:
Some result와 Some (result)는 완전히 동일한 의미입니다.Some result*를 더 권장합니다.일반적으로 Some result처럼 쓰는 것이 더 자연스럽지만, 괄호가 반드시 필요한 경우도 있습니다.
match Some (42, "Hello") with
| Some (num, str) -> Printf.printf "숫자: %d, 문자열: %s\\n" num str
| None -> print_endline "값 없음"
;;
(* 출력: 숫자: 42, 문자열: Hello *)
🚀 설명:
Some (42, "Hello")는 Some이 하나의 튜플 값을 감싸고 있음.Some num, str처럼 괄호 없이 쓰면 구문 오류 발생 (Some이 num만 감싼 것으로 인식됨).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 *)