OOP - How does one create a class in ReasonML
Asked Answered
M

1

9

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.

Marriageable answered 14/9, 2017 at 12:38 Comment(0)
N
11

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];
};
Nickelodeon answered 14/9, 2017 at 13:30 Comment(1)
Classes have their use-cases, they just are pretty rare.Hooves

© 2022 - 2024 — McMap. All rights reserved.