Question

I have made a custom type and would like to create 2 variables that prove my type works as expected.

type number = A of int | B of float;;

let a = 0;;
let b = 0.0;; 

How should I change the variable declarations to force them to type number? Currently a is int and b is float.

Was it helpful?

Solution

To force them to be type number, all you need to do is assign them to a value of type number. The examples you give:

let a = 0
let b = 0.0

are assigning them to values of type int and float respectively. To get values of type number, construct number objects from those values like this:

let a = A(0)
let b = B(0.0)

OTHER TIPS

You just go:

let a = A(0)
let b = B(0.0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top