GithubHelp home page GithubHelp logo

Comments (2)

theartful avatar theartful commented on August 22, 2024

Hi, author of bindable_properties here. First of all, great work you've done here. I wish I found this library sooner.

Qt runs the lambda when the binding is first set, and then stores all properties whose "value" function was called. I'm using the same technique. I'm very surprised by your example not showing this.

#include <QProperty>
#include <assert.h>

int main() {
    QProperty<int> prop1 { 3 };
    QProperty<int> prop2 { 4 };

    QProperty<int> prop3;

    bool prop3_bound_to_prop1 = true;

    prop3.setBinding([&]() {
        if (prop3_bound_to_prop1) {
            return prop1.value();
        } else {
            return prop2.value();
        }
    });

    prop1.setValue(5);
    assert(prop3.value() == 5);

    prop3_bound_to_prop1 = false;
    prop2.setValue(6);

    assert(prop3.value() == 6); // this fails
}

I think it's a really neat trick, but it has the unintended problem you mentioned of not capturing properties in code branches that didn't run. AFAIK Qt doesn't warn about this (neither do I).

from ureact.

YarikTH avatar YarikTH commented on August 22, 2024
#include <QtCore/QDebug>
#include <QtCore/QProperty>

#define LOGGED_EXPR(_EXPR_)                                                    \
  qDebug() << "===================\n" << #_EXPR_;                              \
  _EXPR_

void condition_qproperty() {
  QProperty<int> left(-1);
  QProperty<int> right(1);
  QProperty<bool> is_right(true);

  QProperty<int> result;

  result.setBinding([&]() {
    qDebug() << "result is calculated";
    if (is_right.value())
      return right.value();
    else
      return left.value();
  });

  auto wtf = result.subscribe(
      [&]() { qDebug() << "result is notified: " << result.value(); });

  LOGGED_EXPR(right = 2);
  LOGGED_EXPR(left = -2);
  LOGGED_EXPR(is_right = false);
  LOGGED_EXPR(left = 3);
}

void condition_bool() {
  QProperty<int> left(-1);
  QProperty<int> right(1);
  bool is_right(true);

  QProperty<int> result;

  result.setBinding([&]() {
    qDebug() << "result is calculated";
    if (is_right)
      return right.value();
    else
      return left.value();
  });

  auto wtf = result.subscribe(
      [&]() { qDebug() << "result is notified: " << result.value(); });

  LOGGED_EXPR(right = 2);
  LOGGED_EXPR(left = -2);
  LOGGED_EXPR(is_right = false);
  LOGGED_EXPR(left = 3);
}

int main() {
  qDebug() << "\n------- condition_qproperty ------\n";
  condition_qproperty();
  qDebug() << "\n--------- condition_bool ---------\n";
  condition_bool();
}

It seems that dependency of non-property condition (condition_bool) breaks the system (but it is bad practice in general, at least because non-property has no ways to notify property to update it. And yes, there is no any way to detect and warn it from code.

Also, it seems that used properties are detected not only on the first calculation, but on each of them. In the condition_qproperty example, left is not used for the first time, but become used after we changed condition. Moreover, when we changed condition, then after recalculation, result no longer recalculated after right has changed.
It is pretty neat in terms that value can depend on tens of properties, but be recalculated only in response to few of them that match current conditions.

It is more or less easy to implement. I feared, that it requires some black magic, when I saw BindingFunctionVTable

P.S. But my idea of using property bindings for synced algorithms is not viable. To do things properly, we need to change topology and recalculate lambda. But synced algorithms are not reenter able (for example take and drop... and even fold).

from ureact.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.