Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 2592

Need help with binding to a function with a complex type

$
0
0

Heyall, I’ve recently decided to take a look at rescript and I really like it, and I’ve been using it to rewrite a small thing I have in javascript, and while the rewrite was really successful, I need some help on how to figure out how to write bindings to a particularly complicated function.

So there’s a global function in a utils class which is globally exposed to the js sandbox which the main code runs in, and the class has a function which works kinda like this:

utils.parseParametersFromArguments([{name: "index", type: "number"}, {name: "foo", type: "string"}], ["index:1", "foo:bar"])

In that particular example it returns

{ success: true, parameters: { index: 1, foo: "bar" }, args: [] }

And in case of a failure it would instead return something like this

{ success: false, reply: "Could not parse parameter \"index\"!" }

As you can see, you input a definition and then the arguments, and then it parses into that definition, or else it returns an error. In typescript that type is defined as

type ParameterValueMap = {
	string: string;
	number: number;
	boolean: boolean;
};
type ParameterType = keyof ParameterValueMap;

type ParameterDefinition = {
	readonly name: string;
	readonly type: ParameterType;
};
type ParameterDefinitions = readonly ParameterDefinition[];

static parseParametersFromArguments (
		paramsDefinition: ParameterDefinitions,
		argsArray: string[]
	): { success: true; parameters: Record<string, ParameterValue>; args: string[]; } | ResultFailure {

I was trying to do it kind of like this but i don’t think it’s particularly correct

  type parameterType = @string [ #string(string) | #number(float) | #boolean(bool) ]

  type parameterDefinition<'a> = {
    name: string,
    @as("type") type_: 'a,
  }

  type resultFailure = { success: bool, reply: string }
  type resultSuccess = { success: bool, parameters: Js.Dict.t<parameterType> }

  type resultRecord = {}

  @scope("utils") @val
  external parseParametersFromArguments: (
    array<parameterDefinition<parameterType>>, 
    array<string>
  ) => resultSuccess  = "parseParametersFromArguments"

Viewing all articles
Browse latest Browse all 2592

Trending Articles