Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP - How does one create a class in ReasonML

Tags:

oop

ocaml

reason

I know that in OCaml, one can create a class doing the following:

class stack_of_ints =
  object (self)
    val mutable the_list = ( [] : int list ) (* instance variable *)
    method push x =                        (* push method *)
      the_list <- x :: the_list
  end;;

However, I have been struggling on finding documentation on how to do it in Reason. Thank you.

like image 540
Charlie-Greenman Avatar asked Sep 14 '17 12:09

Charlie-Greenman


People also ask

What is create class OOP?

OOP - Classes and objects - Create Class OOP (Object Oriented Programming) is a programming concept (or technique), which treats data and functions as objects. Important to this concept is to understand the difference between a Class and an Object. - A class is a "blueprint" for an object, is a code template used to generate objects.

What are classes and objects in Oops?

In simple words, classes and objects are the fundamental building blocks of Object-Oriented Programming, which help us to implement several key concepts in OOPS. What is a Class? A class is a user-defined layout or blueprint of an object that describes what a specific kind of object will look like.

What is OOP (Object Oriented Programming)?

Actionscript Course OOP (Object Oriented Programming) is a programming concept (or technique), which treats data and functions as objects. ActionScript 3 contains several predefined classes, such as "Date class" used when working with date and time, "String class" for strings, and others; but you can also create your own classes.

What is OOP and why is it so popular?

The reason OOP is quite popular is because of the way that it offers code reuse. By writing a class, each instance of the class (or object) uses the same code to operate and store its variables; whereas in procedural programming it is often easy to duplicate much of the code.


1 Answers

Classes and objects aren't very well documented because these features add a lot of complexity for (usually) very little benefit compared to a more idiomatic approach. But if you know the OCaml syntax for something, you can always see what the Reason equivalent is by converting it with the online "Try Reason" playground. See your example here, which gives us this:

class stack_of_ints = {
  as self;
  val mutable the_list: list int = []; /* instance variable */
  pub push x =>
    /* push method */
    the_list = [x, ...the_list];
};
like image 69
glennsl Avatar answered Oct 06 '22 04:10

glennsl