Вопрос

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.

Это было полезно?

Решение

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)

Другие советы

You just go:

let a = A(0)
let b = B(0.0)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top