0

I'm looking to see if i can genericize the following code a bit.

type recordType = [
  | `Todo(todo, idFunction)
  | `TodoItem(todoItem, idFunction)


let commitItemToSchema = (normalizedSchema, recordType) => {
  switch(recordType){
  | `Todo    (todo,     idFun) => {...normalizedSchema, todo:       addOrModifyById(normalizedSchema.todo,       todo,       idFun)}
  | `TodoItem(todoItem, idFun) => {...normalizedSchema, todoItem:   addOrModifyById(normalizedSchema.todoItem,   todoItem,   idFun)}
  };
};

Is there a way that I can get the \'Todo or the 'TodoItem from the variant as a variable?

Thanks

GTDev
  • 5,488
  • 9
  • 49
  • 84

2 Answers2

0

It would be helpful if you showed us the complete set of types. OCaml/BuckleScript/ReasonML do support polymorphic types. Here is simple type and value declaration using a polymorphic type in ReasonML.

type wrapper('a) = {
  item: 'a,
  details: string
};

let x = {item: 1, details: "This is an int"};

Here are two kinds of functions that take a wrapper with an unrestricted type 'a and a wrapper with a restricted type 'a as int.

let unrestrictedType = (x: wrapper('a)) => {
  Js.log(x);
};

let restrictedType = (x: wrapper(int)) => {
  Js.log(x);
};
MCH
  • 2,124
  • 1
  • 19
  • 34
-1

If the variant could be passed as variable, it would lose type check in that case. I don't think ReasonML support that.

Yan
  • 19
  • 1