Record type ocaml and null

I am trying to determine type type:

type aaa = NULL | {a: int; b: int} ;;

But the compiler does not allow this. I am not sure why we cannot mix record type with anything else.

I need to match the value if it is a record type or a null record, and I'm tired of creating a dummy record, for example {a = -999; b = -999}.

is there a better way?

+3
source share
2 answers

Part of the record "record" must be performed by a separate type. You can then wrap this in the type "option" if you want to express "None" or "Some value".

type aaa = {a: int; b: int}
type bbb = aaa option
+7
source

: Objective Caml . :

type test = Null | {a : int ; b: int }

let value = { a: 0 ; b : 42 } in print_int value.a

, value Null, a. , Objective Caml . , .

, :

type test = Null | Pair of { a : int ; b : int }

match Pair { a : 0 ; b : 42 } with
  | Null -> 0
  | Pair p -> p.a

p ? , , , , , , , (, < ; ... >, #type value :> type ).

, . , , :

type test = Null | Pair of int * int
+7

Source: https://habr.com/ru/post/1727207/


All Articles