lorents / rtl

RTL - Reactive Template Library for C++
MIT License
4 stars 0 forks source link

Consider extracting `fun` from `var` #2

Open lorents opened 2 years ago

lorents commented 2 years ago
var<float> val = 3.14f;
ref<int> val2 = val.convert([](float v){ return floor(v); }, [](int i){ return (float)i; });
ref<int> val3 = val2.convert([](int v){ return v+1;}, [](int i){ return i - |; });
ref<int> foo = 20.0f;
ref<int> foo2 = foo;

// ref can animate, set and get

var<float> val = 13.37f;
fun<float> val2 = {[](){ return val * 2.0f; }};
fun<float> val3 = {[](){ return val2 + 2.0f; }};
fun<int> foo = 10.0f;
fun<int> foo2 = foo;

// fun can only get, not animate or set

the discrepancy in the interface is due to the ref requiring a single context, while fun can use the lang context.

i think it might be useful to know whether something has a state or is derived in the type.

if you take a read-only parameter you take fun, if you need two way parameter you take ref

lorents commented 1 year ago

Since this issue was made, an internal version has introduced this under the name rtl::get, and it's literally just an std::function<T()> with some implicit conversion, but it does the trick. Will push this soon.