How to represent a null value in a float type for Ocaml

I know this may seem very simple, but basically, I want to say that in the template

match value with Null-> failwith "Empty" |value-> #do something 

I tried any variation of null or none, and also tried an element that cannot be used because the value is float.

I'm at a standstill and any help would be appreciated

+4
source share
2 answers

You can not. This is a design choice. Many languages ​​allow null. The problem with this approach is that the values ​​are zero when the programmer does not expect this, or the code should be studded with checking each input value for zeros.

OCaml uses an approach that, if the value can be null, then it should be explicitly marked as such. This is done with the option type:

 match value with | None -> failwith "Empty" | Some value -> (* do something *) 

However, if you replace it directly in your program, if it is not compiled, because OCaml will detect that the "value" cannot actually be null. Whatever is created needs to be updated to indicate when it returns "null":

 let safe_divide numerator denominator = if denominator <> 0. then Some (numerator /. denominator) else None (* division by zero *) 
+10
source

As Delnan said, there is no null in OCaml. What you can do if this suits your problem is to use options in a template like this:

 let your_function x = match x with | None -> failwith "Empty" | Some f -> #do something 
+2
source

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


All Articles