Charlie-Greenman
Charlie-Greenman

Reputation: 1659

How to create internal value in a module using OCaml/Reason

I have the following module:

type userBuilderType = {
  mutable name: string,
};

module BuilderPattern = {
  let builder () => {
    name: "",
  };
  let setName = fun name => builder.name = name;
  let getName = builder.name;
};

BuilderPattern.setName("Charles");
Js.log(BuilderPattern.getName);

It accomplishes the following:

  1. Creates type for setter
  2. builderName object for setting + getting In addition I would like to:
  3. Be able to retrieve name using a JS.log on getName function

However, in this instance I get back the following error:

This is: unit => userBuilderType But somewhere wanted: userBuilderType 

Any suggestions as to how I properly set up the setter/getter is more than appreciated. Thank you.

Upvotes: 4

Views: 184

Answers (1)

glennsl
glennsl

Reputation: 29106

There are numerous problems with the code in your question, several of them purely syntactic. The github code makes a bit more sense, but the main problem you have is that you don't make an instance of your data anywhere.

I think this is what you want, where an instance is constructed and passed to the getter and setter:

type userBuilderType = {
  mutable name: string,
  mutable age: int,
  mutable phone: string,
  mutable address: string
};

module BuilderPattern = {
  let builder () => {
    name: "",
    age: 0,
    phone: "",
    address: ""
  };
  let setName builder name => builder.name = name;
  let getName builder => builder.name;
};

let myBuilder = BuilderPattern.builder ();

BuilderPattern.setName myBuilder "Charles";
Js.log(BuilderPattern.getName myBuilder);

But this also seems like a valid interpretation of your intent, where the state is kept globally (which is usually a very, very bad idea):

type userBuilderType = {
  mutable name: string,
  mutable age: int,
  mutable phone: string,
  mutable address: string
};

module BuilderPattern = {
  let builder = {
    name: "",
    age: 0,
    phone: "",
    address: ""
  };
  let setName name => builder.name = name;
  let getName () => builder.name;
};

BuilderPattern.setName "Charles";
Js.log(BuilderPattern.getName ());

In both cases the problem boils down to confusing a function taking a single unit argument with just a let binding, in various ways.

Upvotes: 2

Related Questions