in Education by
I have the following function [@bs.obj] external route: ( ~_method: string, ~path: string, ~action: list(string) => unit, ~options: Js.t({..})=?, unit ) => _ = ""; Since functions can be partially applied, I expect to be able to do this: let get = route(~_method="GET"); but it gives me this error: This expression's type contains type variables that can't be generalized: (~path: string, ~action: list(string) => unit, ~options: {_.. }=?, unit) => {. "_method": string, "action": list(string) => unit, "options": Js.undefined({.. }), "path": string} What am I doing wrong here? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
This is not actually about the optional parameters and currying, but about value restriction and non-generalized, aka weak, type variables. TL;DR; either turn get into a syntactic function by adding a parameter, e.g., let get () = route(~_method="GET") ();, or create a *.rei interface file for your module. Longer Story The .. row variable denotes a polymorphic type that the compiler couldn't reduce to a normal monomorphic type (since there is evidently no usage of this function) nor it can trust that the partial application route(~_method="GET") has not actually already accessed the options parameter and may be stored somewhere in it, which should define the type. Therefore, the compiler, can't leave it as a polymorphic variable, nor can it give a concrete type, as a result, it produces a weak type variable, which could be seen as a reference cell for the future defined concrete type. Like an uninitialized type. It will be initialized later, by the code that uses the get function. If, at the end of the day, the type is never used, it may escape the scope of the module, which is forbidden by the OCaml/Reason typing rules. Therefore, you shall either give it a monotype manually (i.e., constraint it to some monomorphic type), or create an interface file where this value is hidden (i.e., not present), and therefore couldn't leak the scope of the module. Basically, just creating an empty .mli/.rei file with the same name as your .ml/.re file will resolve this issue. Another common solution is to turn get into a syntactic function, i.e., something with syntactically explicit variables, e.g., let get () = route(~_method="GET") (); Further Reading https://ocamlverse.github.io/content/weak_type_variables.html http://caml.inria.fr/pub/docs/manual-ocaml/polymorphism.html#sec51 https://v1.realworldocaml.org/v1/en/html/imperative-programming-1.html#side-effects-and-weak-polymorphism

Related questions

0 votes
    Mention what is the command to interpolate two objects in d3.js?...
asked Nov 14, 2020 in Technology by JackTerrance
0 votes
    I am writing a web server with extensive usage of reactive programming. I noticed that I forgot to check ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    ________ function generates n normal random numbers based on the mean and standard deviation arguments passed to ... R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    In which model Machine generates mapping Function AlgorithmRequired to answer. Single choice. (1 Point) Select the correct answer from above options...
asked Dec 27, 2021 in Education by JackTerrance
0 votes
    In which model Machine generates mapping Function AlgorithmRequired to answer. Single choice. (1 Point) Select the correct answer from above options...
asked Dec 26, 2021 in Education by JackTerrance
0 votes
    What is true about functions : I) Functions are objects II) Can be assigned to a variable III) Can be anonymous IV) Return value type has ... , III 3. I, II, III, IV 4. I, II, IV...
asked Feb 25, 2021 in Technology by JackTerrance
0 votes
    What is true about functions : I) Functions are objects II) Can be assigned to a variable III) Can be anonymous IV) Return value type has ... , III C) I, II, III, IV D) I, II, IV...
asked Oct 9, 2020 in Technology by JackTerrance
0 votes
    If I have a "Bill" entity, I might create that bill instance via REST API by assigning the food ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    If I have a "Bill" entity, I might create that bill instance via REST API by assigning the food ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I have a vue.js application and into a componente there's the data method thats returns a nested object ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Write the names of microbes found in the following food materials. yogurt, bread, root nodules of ... ,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    When does the function name become optional in JavaScript? (a) When the function is defined as a looping ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    For a T-network if the Short circuit admittance parameters are given as y11, y21, y12, y22, then y11 in terms ... for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 13, 2021 in Education by JackTerrance
...