Private Methods

In this lesson, we'll learn how to implement private functions in an object.

What are Private Methods?

In the last lesson, we implemented the rectangle type and created an object out of it. All of its functions were public.

Press + to interact
type rectangle = {
.
getColor: string,
getLength: float,
getWidth: float,
getArea: float
};
let rect: rectangle = {
/* Values */
val l = 10.5;
val w = 5.5;
val c = "Blue";
/* Function definitions */
pub getColor = c;
pub getWidth = w;
pub getLength = l;
pub getArea = this#getWidth *. this#getLength
};
Js.log(rect#getColor);
Js.log(rect#getArea);

Sometimes, we need to create functions for our own ease. These ...