As to your previous question it did not error at the 4th line because at that point you had not tried to call the push property; you were just printing it, and Js.log prints anything you give it–it doesn’t care about the type.
This has type: 'a => unit
Somewhere wanted: Js_OO.Meth.arity1<'b>
Unfortunately the example in the doc isn’t incredibly useful as I need to bind to the method belonging to an object returned by ES6 module, not global document object. I did get it to work though:
type history
@bs.send external push: (history, string) => unit = "push"
@bs.module("@core") external browserHistory: history = "browserHistory"
...
push(browserHistory, "https://example.com")
You mentioned methods and functions having different types in ReScript. I couldn’t find any mention of methods in the docs, how are they supported? The following doesn’t seem to work:
let browserHistory = { "push": href => Js.log(href) }
browserHistory["push"]("https://example.com")
It looks like you are using a JS object type and you want to use its function as a method, so I guess you need to use the @meth decorator on the right attribute to mark it as a method.
Example:
type history = { @meth "push": string => unit }
@bs.module("@core") external browserHistory: history = "browserHistory"
browserHistory["push"]("test")
The way you bound it is almost exactly how I would have done it, with @bs.module. The only doubt in my mind is, is @core a valid module? Shouldn’t it be @core/something?
I can definitely see merit for both approaches, it comes down to personal preference I guess.
As for the @core module, it’s just an alias mapped to another directory under the same source tree in order to make my life a bit easier (babel-plugin-module-resolver). Just need to be careful not to map to any actual npm modules, eh
Still, why is everyone dodging my methods-in-pure-rescript question?
Not sure what you mean by “methods in pure ReScript”. The recommended way doing OOP is using @send and ->, which is essentially doing the same thing, but with less extra concepts.
Thank you for the clarification, that’s exactly what I needed to know. It’s definitely a paradigm shift for me as I have been so accustomed to passing around functions in structured objects. Marking it as solved now.