Search⌘ K

Solution Review: The Calculator Object

Explore how to create a closed object in ReasonML, using the dot operator for type definitions. Learn to declare methods with specified parameters and implement them in a calculator object example.

We'll cover the following...

Solution

Reason
/* Type definition */
type calculator = {. /* The . operator to indicate the closed object */
add: (float, float) => float,
subtract: (float, float) => float,
multiply: (float, float) => float,
divide: (float, float) => float
};
/* Object Creation */
let cal: calculator = {
/* Public methods */
pub add = (first, second) => first +. second;
pub subtract = (first, second) => first -. second;
pub multiply = (first, second) => first *. second;
pub divide = (first, second) => first /. second;
};
Js.log(cal#add(10.5, 5.0));
Js.log(cal#subtract(10.5, 5.0));
Js.log(cal#multiply(10.5, 5.0));
Js.log(cal#divide(10.5, 5.0));

Explanation

Since ...