Defining a JS class in rescript

Is a rescript-equivalent of this JS code (src/item.js · main · Philip Chimento / Bloatpad · GitLab) possible?

The second argument to GObject.registerClass is a class.

// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2021 Philip Chimento

import GObject from "gi://GObject";
import Gtk from "gi://Gtk";

export const NotesListItem = GObject.registerClass(
  {
    GTypeName: "BloatpadNotesListItem",
    // ...
  },
  class NotesListItem extends Gtk.Grid {
    get title() {
      return this._title.label;
    }

    set title(value) {
      if (this._title.label === value) return;
      this._title.label = value;
      this.notify("title");
    }

    get excerpt() {
      return this._excerpt.label;
    }

    set excerpt(value) {
      if (this._excerpt.label === value) return;
      this._excerpt.label = value;
      this.notify("excerpt");
    }

    bind(note) {
      const f = GObject.BindingFlags.SYNC_CREATE;
      this._titleBinding = note.bind_property("title", this, "title", f);
      this._excerptBinding = note.bind_property("excerpt", this, "excerpt", f);
    }

    cleanup() {
      if (this._titleBinding) this._titleBinding.unbind();
      if (this._excerptBinding) this._excerptBinding.unbind();
    }
  }
);

This is possible if you’ll use ES5 syntax for classes that ReScript supports. Won’t be easy though as doing inheritance and getters/setters makes it extra hard.

I would start by translating ES6 code to ES5 using babel . And then try to translate the generated ES5 to ReScript.

You can also try turning on the loose mode to make ES5 output simpler, but it might not work depending on how Gtk.Grid is implemented and what GObject.registerClass expects.

Hmm. Even this simple example is too complex for me to warrant further exploration.

This is one area where F#-Fable might be a better alternative to ReScript.

All this can be wrapped in a library. There’re many such libraries for JavaScript from the old days, but I don’t know of any particular one that I could recommend.