Comments (2)
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.
#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)
- Rework observer/scoped_observer/observe/tap
- Consider adding `transaction::finish()` method HOT 1
- Consider to support iterators as an observer argument HOT 1
- Make sure transactions can't be started or finished when turn is in progress HOT 1
- `adjustment` adapter possible improvements
- Make `Adaptor` `AdaptorClosure` and nodes part of public interface
- Try to mesure and improve compile times HOT 8
- Get rid of `ureact_amalgamated.hpp` HOT 1
- Make normal library mode (not header-only)
- First code example in doc/introduction.adoc does not compile HOT 5
- Consider adding dereference operator for signals
- Consider `connection_owner` idea from `PG1003/observer`
- Compile errors for Visual Studio HOT 4
- Get rid of `issue_template.md` HOT 1
- Rework github actions
- `ureact::merge` can't compile in latest MSVC in c++20 mode HOT 3
- Consider making "event_source::operator<<" transaction wrapper HOT 1
- Research signal-slot library features
- Consider pitchfork project structure (aka PFL)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ureact.