For example, I have an es6-like class:
class Foo {...}
And I want to extend it:
class Bar extends Foo {...}
In reason-react documentation I found examples, but I'm not sure that it's appropriate for me:
let component = ReasonReact.reducerComponent "TodoAppRe";
let make _children => {
...component,
initialState: fun () => {count: 0},
<...>
But when i try to write code in this style, i get an error:
let myclass unit => {
...mysuperclass,
hello: fun () => {
Js.log "FooBar";
}
};
Error: Unbound record field update
(In this example mysuperclass
is external from 3rd-party js-library).
Maybe I am doing something wrong?
let foo bar => { ...baz, quux: ... }
is not a syntax for inheritance, it's a syntax for composition. Specifically, it's taking thebaz
record value (not object) and updating itsquux
member (not method).If you want to extend a JS class in Reason/BuckleScript, first keep in mind that BuckleScript generates code that's backwards-compatible with ES5, so that you don't need to use Babel. In fact BuckleScript doesn't directly support extending a class.
But, you can include raw JavaScript in your Reason source code using BuckleScript's
[%%bs.raw]
attribute ( https://bucklescript.github.io/bucklescript/Manual.html#_embedding_raw_js_code_as_statements ):And then you can write a binding to the
Dog
class in the code below that:But, remember that since this ES2015, you will need to run it through Babel if you want it to be backwards-compatible with pre-ES2015 browsers.