Advertisement

programming language idea

Started by July 02, 2010 03:48 AM
13 comments, last by RivieraKid 14 years, 4 months ago
var x = 5
rvar y(x) = print(x)

this code, by use of the keyword rvar (reactive variable), would re-evaluate y when x changes (resulting in printing x).

This is similar to how Excel works -> if you change any cell the other cells update.

What do you think?
Does any language do this at the moment?
What would it take to include this feature into an existing language?
Quote: Original post by RivieraKid
var x = 5
rvar y(x) = print(x)

this code, by use of the keyword rvar (reactive variable), would re-evaluate y when x changes (resulting in printing x).

This is similar to how Excel works -> if you change any cell the other cells update.

What do you think?
Does any language do this at the moment?
What would it take to include this feature into an existing language?


You can do something similar to this with operator overloading and function pointers in c++.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Advertisement
In C++, it would be simple enough to make a class that, through operator overloading, does whatever action you want.
Using templates, you could make that class extensible to manage any other class that supports those same operators.
The end result would look something like
template<typename T>void print( T &v ){  std::cout << v << std::endl;}....cvar<float> x;x.onChange( print<float> );....x = 5; // calls "print<float>(5)"

I chose the name "cvar" because this is fairly common functionality for the "quake console" style variable/function blackboards.
not exactly what i meant

var x = 5
var y = 10
rvar z(x,y) = x * y
-> z is 50

x = 2
-> z is 20

y = 5
-> z is 10

i've already implemented this in C# for int's but its not a generic solution
I think you are talking about lazy evaluation, which is used in some languages (usually functional) like Haskell.
yes, a bit, but I want my reactive variable / function to execute because I want it to execute when certain variables change, not when a subroutine uses that variable / function

In some design patterns we have events like "PropertyChanged" which fire when we change properties. We hook onto these events and update the UI. This results in alot of bloat code and more bugs.

Basically, your code has a dependency, when the dependency changes we react to that and recompute the dependant result.
Advertisement
The language JavaFX (http://javafx.com/docs/howto/Binding-Tutorial.jsp) does this.

They call it binding. Microsoft .NET also has some interface that supports this, though it is more complicated.

In JavaFX, you write:
var x = 5;var y = bind x * 2;

Then, whenever x changes, y is reevaluated.

For functions, you use the bound keyword.
Well, Lisp has this. More stuff here.
Quote: Original post by RivieraKid
yes, a bit, but I want my reactive variable / function to execute because I want it to execute when certain variables change, not when a subroutine uses that variable / function

In some design patterns we have events like "PropertyChanged" which fire when we change properties. We hook onto these events and update the UI. This results in alot of bloat code and more bugs.

Basically, your code has a dependency, when the dependency changes we react to that and recompute the dependant result.

What scenario do you have in mind exactly?

Either there are side effects, or there aren't. If there are no side effects, then it doesn't matter whether the rvar is recomputed when its dependencies change or when its value is queried. If there are side-effects, it seems to me like you're looking for trouble.

Aren't rvar's a subset of what dependency properties do in WPF? I did something similar for use with WPF: I have an IObservable<T> interface,
public interface IObservable<T> : INotifyPropertyChanged{    new T Value    {        get;    }}

a mutable ObservableVariable<T> : IObservable<T> class (with a Value setter), and a series of Derived<T1, ..., Tn, U> : IObservable<U> classes, which represent values which recompute themselves when one of their dependencies change.
_filled = new ObservableVariable<bool>( false );_fill = new Derived<bool, Brush>( b => b ? Brushes.Black : Brushes.White, _filled );

(I know I could just use dependency properties, or setters and styles/control templates, or value converters for this, but this approach is lightweight and flexible enough for my own purposes)


More importantly, why make it a language feature? Just for the easy syntax? Or do you have any kinds of compile-time analyses (correctness, optimizations) in mind?
I believe the word you're looking for is "function". In python, you can do this with "y = lambda: x" so y() would always equal the variable x. You could also make a class with overloaded operators to do it with a slightly nicer notation. You can also use a computer algebra system, such as SymPy, to form symbolic relationships between variables, constants, and other entities.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk

This topic is closed to new replies.

Advertisement