DEV Community

Discussion on: Immutable Updates of Objects in Reason

Collapse
 
idkjs profile image
Alain

Awesome series! Really. Thanks.

I can't seem to log myImmutableCat or myNewCat. Is there a trick to that?

Thank again.

Collapse
 
rawtoast profile image
RawToast

I presume because objects don't print as nicely as records. As reason will try to print the methods alongside the values.

Js.log2("Does this work", myNewCat);

"Does this work" [[4,15,null,-278822339,null,243512077,null,461513217,null,588852681],16,"Cool Cat",5,"Dave",5]

One way would be to add a show method to all your objects, where you manually make a string.

class immutableCat(name, age) = {
  as _;
  val name = name;
  val age = age;
  pub show = "Cat {name: " ++ name ++ ", age: " ++ string_of_int(age) ++ "}";
};

let myImmutableCat: immutableCat = new immutableCat("Cool Cat", 5);

let show = obj => obj#show;  // put this in a Utility module somewhere
Js.log2("Does this work", show(myImmutableCat));