Skip to main content
GameDev.net gamedev.net
🔒 Locked

programming language idea

Started by RivieraKid Jul 2, 2010 at 3:48 AM 13 replies 2k views
Original Post
RivieraKid
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?
SimonForsman
SimonForsman
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!
KulSeran
KulSeran
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.
RivieraKid
RivieraKid
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
awefdbgb
awefdbgb
I think you are talking about lazy evaluation, which is used in some languages (usually functional) like Haskell.
RivieraKid
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.
SamLowry
SamLowry
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 interface,
public interface IObservable<T> : INotifyPropertyChanged{    new T Value    {        get;    }}

a mutable ObservableVariable : IObservable class (with a Value setter), and a series of Derived : IObservable 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?
Extrarius
Extrarius
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
benryves
benryves
It seems that you just want a function. If you want something that looks like less of a function call, feel free to wrap it up in class:

class Program {    class ReactiveVariable<T> {        Func<T> function;        public ReactiveVariable(Func<T> function) {            this.function = function;        }        public static implicit operator T(ReactiveVariable<T> rv){            return rv.function();        }        public override string ToString() {            return this.function().ToString();        }    }    static void Main(string[] args) {        var x = 5;        var y = 10;        var z = new ReactiveVariable<int>(() => x * y);                    Console.WriteLine(z); // z == 50                    x = 2;        Console.WriteLine(z); // z == 20                    y = 5;        Console.WriteLine(z); // z == 10        // Implicit conversions work too.        int i = z;        Console.WriteLine(i);                }}

Edit: Once converted to a regular int you do of course lose the magic of the function call, so I'd be tempted to stick with the function call or at least make the conversion operator explicit.

[Edited by - benryves on July 2, 2010 9:52:24 AM]
[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]
RivieraKid
RivieraKid
I am aware of the WPF and Silverlight binding syntax (i am a SL developer) but they are just classes which wrap up events.

Xaml just binds to the PropertyChanged event and uses reflection to Get the properties. Its an ugly hack. Dependency properties are there to support xaml.

JavaFX is what I was looking for. Thank you.

benryves, in your example z only re-evaluates when you invoke it explicitly.
Katie
Katie
There is a language called Eden which works in a similar way.

http://www2.warwick.ac.uk/fac/sci/dcs/research/em/software/eden/

It does, in fact, work in the way you describe -- like cells in a database. The software is available for download and will apparently compile on Linux.
superpig
superpig
If the variable gets re-evaluated long before I use it, why should I care?

Also, what happens if I do:

var x = 5
rvar y(x) = x++
Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse
RivieraKid
RivieraKid
it could be a function (like an event handler) or it could be onscreen.

stack overflow exception

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.