我已經定義了一些類型:功能OCaml中
type box = Box of int
type table = Table of int
type compare_result = Lt | Eq | Gt
看來,OCaml中,我們不能定義2個功能與同名但不同類型的參數:
let compare (a: box) (b: box): compare_result = (...)
let compare (a: table) (b: table): compare_result = (...)
let res_box = compare (Box 1) (Box 2) in (* which is supposed to call the first funciton *)
let res_table = compare (Table 1) (Table 2) in (* which is supposed to call the second function *)
所以任何人都可以告訴我在OCaml中做什麼替代方法?我們必須以不同的方式命名這兩個功能嗎?
請注意,您的第二個'compare'聲明將隱藏前一個(在Ocaml中沒有函數重載)。 – akoprowski