Search⌘ K

Private Methods

Explore how to define and use private methods in ReasonML objects to keep internal functions hidden while supporting public methods. Understand the concept of private methods through a practical example with a rectangle object, enhancing your knowledge of object-oriented programming in ReasonML.

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.

Reason
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 ...