DEV Community

Discussion on: The Quest for ReactiveScript

Collapse
 
ninjin profile image
Jin

Idea about double-destiny operator:

var a = 10;
var b <=> a + 1;

a = 20;
Assert.AreEqual(21, b);

b = 20;
Assert.AreEqual(19, a);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ryansolid profile image
Ryan Carniato

That's interesting. Scares me a bit. Part of me really wants to make mutation(assignment) special and isolated but I just might not be letting go enough to fully embrace this thinking. Would it ever be difficult to reverse the derivations? Sure subtracting 1 from b is easy enough. I just wonder if that wouldn't always be the case.

Collapse
 
3shain profile image
3Shain

It only makes sense if the mapping is a bijection (math term). It's a really rare property, meaning zero information loss.

Thread Thread
 
ninjin profile image
Jin

No, It's very common. We can bind local property with part of json which bind with local storage as example. So write to property will change json at local storage and affects to same property of another instance of same app. Example:

class Profile {

    @ $mol_mem
    store() {
        return new $mol_store_local({
            profile: { name: 'Anon' }
        })
    }

    @ $mol_mem
    name( next?: string ) {
        return this.store().sub( 'profile' ).value( 'name', next )
    }

}

const profile = new Profile

profile.name() // 'Anon'
profile.name( 'Jin' ) // 'Jin'
// restart app
profile.name() // 'Jin'
localStorage.getItem( 'profile', '{ "name": "Anon" }' )
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
3shain profile image
3Shain

Bi-directional bindings re-invented? Fair enough.

Collapse
 
ninjin profile image
Jin • Edited

It's the lens in general. See JS example:

let _a = 10
const a = ( next = 10 )=> return _a = next
const b = ( next )=> a( next === undefined ? undefined : next - 1 ) + 1

a(20);
Assert.AreEqual(21, b());

b(20);
Assert.AreEqual(19, a());
Enter fullscreen mode Exit fullscreen mode

We actively use this that approach in this way:

class App {

    // it's signal
    @ $mol_mem
    static a( next = 10 ) { return next }

    // it's derivation but with same api as signal
    @ $mol_mem
    static b( next ) {
        return this.a( next === undefined ? undefined : next - 1 ) + 1
    }

}

App.a(20);
Assert.AreEqual(21, App.b());

App.b(20);
Assert.AreEqual(19, App.a());

Enter fullscreen mode Exit fullscreen mode
Collapse
 
lukechu10 profile image
Luke Chu

This would be impossible to implement in the general case because the compiler would essentially be solving an equation. In most cases, this would be impossible because there could be multiple solutions for a depending on the value of b. In other cases, reversing the operation would simply be impossible, e.g. a hash function.

Collapse
 
azrizhaziq profile image
Azriz Jasni

How about a new keyword?

var a = 10;
reactive b = a + 1; // of course to long :D

a = 20;
Assert.AreEqual(21, b);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ninjin profile image
Jin • Edited
var a = 10;
ever b = a + 1; // not so long

a = 20;
Assert.AreEqual(21, b);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ryansolid profile image
Ryan Carniato

Then we are looking at a label/keyword example and that all applies. My point is it is easy to bucket all solutions into these categories or hybrids of them. We can debate the exact naming/syntax but I have been wondering if we can escape this altogether.