0

I want to generate javascript function called Publisher from ReasonML so that i can use it in other files as for example:

const publisher = new Publisher("Prasad", "email@email.com", "team@email.com", "rill")
const req = Publisher.toAPI(publisher) //  returns {name: "Prasad", email: "email@email.com", team: "team@email.com", service: "rill"}

To achieve above functionality, I wrote ReasonML code in file named Util.re which is:

type publisher = {
  name: string,
  emailID: string,
  teamEmailID: string,
  serviceName: string,
};

type publisherReqBody = {
  name: string,
  email: string,
  team: string,
  publisher: string,
};

module Publisher = {
  let toAPI = (p: publisher) => {
    name: p.name,
    email: p.emailID,
    team: p.teamEmailID,
    publisher: p.serviceName,
  };
  [@bs.new] external create: unit => publisher = "Publisher";
};


After compilation from ReasonML to JavaScript using BuckleScript, what i got

// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE


function toAPI(p) {
  return {
          name: p.name,
          email: p.emailID,
          team: p.teamEmailID,
          publisher: p.serviceName
        };
}

var Publisher = {
  toAPI: toAPI
};

export {
  Publisher ,

}

I am not sure why [@bs.new] external create: unit => publisher = "Publisher"; line not working. I have tried for an hour but no use.

My question:

how to achieve functionality i mentioned in first snippet in JavaScript which is compiled from ReasonML

Thanks a lot!

REDDY PRASAD
  • 1,309
  • 2
  • 14
  • 29
  • 3
    Note that I answered your question in the forum. Please ask in one place at a time or at least link to the other places so we don't duplicate effort :-) https://reasonml.chat/t/javascript-constructor-behaviour-in-reasonml-using-bucklescript/2116 – Yawar Jan 03 '20 at 15:35

1 Answers1

1

The external definition is a description of how to use a JavaScript value, not a value by itself. When you use that external, it will inline the correct code in-place.

If you do let publisher = Publisher.create(); below your snippet, you'll see that the generated code, at call site will be:

var publisher = new Publisher();
bloodyowl
  • 11
  • 1