0

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.

Ruben Bartelink
  • 55,135
  • 22
  • 172
  • 222
jth41
  • 3,542
  • 8
  • 50
  • 105

2 Answers2

5

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)
N_A
  • 19,387
  • 4
  • 47
  • 95
3

You just go:

let a = A(0)
let b = B(0.0)
John Palmer
  • 24,880
  • 3
  • 45
  • 66