4
votes

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.

1
Can you include the tests you run that lead you to belive that the update is not performed? - gallais
Yes will look into, and report back in less than 6 hours. - Charlie-Greenman
Oh, I see. Posting the type error from the get go is also useful. In this case builderName is a function from unit to userBuilderType. You can't access its field directly. If you write let builderName => { ... instead, it ought to work. - gallais
There is a downvote on this comment. As usual, I think it is more beneficial to comment what part of the question caused you to downvote. I would be more than happy to tidy it up. Thank you. - Charlie-Greenman
You are required to post your code here, not GitHub or any other site. minimal reproducible example - Rob

1 Answers

2
votes

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.