Instead of passing a variable number of arguments, we pass an array: external max : array(int) => int = "Math.max" īinding to a value from a module: bs.module BuckleScript supports typing homogeneous variadic arguments. In JS, it is quite common to have a function take variadic arguments. Just for the fun, take a look at how simple and clean the transpiled js code: type external bob : person = external get_age : person => int = external set_age : (person, int) => unit = "age" Now, here is how we can create setters and getters to this javascript object (Thank you from Reason Discord!): type external bob : person = external get_age : person => int = external set_age : (person, int) => unit = "age" Let’s define a javascript object inside a javascript klipse snippet: var bob = This attribute helps get and set the property of a JavaScript object. Here is how we can have a dynamic access to a JavaScript property: type external create : int => js_array = external get : js_array => int => int = external set : js_array => int => int => unit = "" Binding to dynamic key access/set: bs.set_index and bs.get_index If you are curious about the attribute in the callback, see Binding to callbacks (high-order function). In case, you are not familiar yet with Ocaml/Reason pipe operator, here is the transpiled js code: : array('a)] external map : ('a => 'b)) => array('b) = "" Type external get_by_id : dom => string => element = "getElementById" īs.send.pipe is similar to bs.send except that the first argument, i.e, the object, is put in the position of last argument to help user write in a chaining style: : array('a)] external map : ('a => 'b)) => array('b) = "" type external create_date : unit => date = "Date" īinding to method: bs.send and bs.send.pipeīs.send allows us to call a method of a JS object.įor instance, this is how we can bind dom.getElementByIdĮlement is an abstract type fot the element type external dom : dom = "document" Let a = encodeURIComponent("Hello World\n") īinding to JavaScript constructor: bs.newīs.new is used to create a JavaScript object. When the assigned name is exactly the same as the original name, we can leave the name empty: external encodeURIComponent : string => string = "" Here is how we make the Math.sqrt function accessible to our reason code: external sqrt : float => float = "Math.sqrt" The purpose of this article is to expose the main features of Reason FFI in a simple way in order to help you overcoming the difficulty of the syntax. It is a bit complex to understand how it works. For that purpose, we have to write type declarations.įFI in Reason is very powerful but this power comes at a price. In the context of reason, in order to access JS code, we have to build type-safe bindings from JS to Reason. More specifically, this is called FFI (foreign function interface).Ī FFI is a mechanism by which a program written in one programming language can call routines or make use of services written in another. This is part of what is called the Javascript interop. Sometimes, we need to be able to access JS code from our reason code.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |