...

/

Solution Review: The Calculator Object

Solution Review: The Calculator Object

This lesson explains the solution to the calculator object exercise.

We'll cover the following...

Solution

Press + to interact
/* 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 ...