I'm thinking about a language concept. Dunno if it already exists or what. Just a random idea. For convenience, let's call it Rall (for Random Async/Lazy Language). For simplicity's sake, I've made the syntax similar to Python or Ruby or CoffeeScript.
In a normal language:
# doA() will run, complete,
# and return.
doA()
# Then doB() will run, complete,
# and return.
doB()
# And then doC() will run,
# complete, and return.
doC()
In Rall, this would be:
# doA() is launched.
doA()
# doB() is launched.
doB()
# doC() is launched.
doC()
# …and sometime in the future, they
# will complete and return.
In other words, these are all asynchronous. On a single machine, with a single 1-core processor, the two programs will be pretty much the same. Add to any of these quantities and it starts getting interesting. But let's keep to the matter at hand.
Python-like:
def add(var1, var2):
return var1 + var2
# Technically, it runs, but
# it doesn't do anything.
add(1, 1)
# More interesting:
var3 = add(1, 2)
# Oh oh:
total = add(var3, 5)
print "%d tomatoes." % total
I take it you can count. Rall version:
# This doesn't actually
# define a function. It
# queues a function to be
# defined later.
def add(var1, var2):
return var1 + var2
# This checks if the queue
# contains a function def
# for 'add'. If no, error.
# If yes, eval it, then run
# with these arguments.
add(1, 1)
# This queues a variable to
# be defined later.
var3 = add(1, 2)
# This queues another var
# to be defined later.
total = add(var3, 5)
# Aha! This checks if there
# is a 'total' variable on
# the queue (or if it has
# already been defined).
# If no, error. If yes, “run”
# it.
#
# This should in turn check
# two things: the 'add' function
# (which *is* defined, so skip)
# and the 'var3' variable (which
# isn't, so “run”).
#
# When we're done going back up
# the chain, and defining all the
# things, we can print the value:
print "%d potatoes." % total
This is actually pretty safe. Now let's do this:
def add(var1, var2):
return var1 + var2
add(1, 1)
var3 = add(1, 2)
total = add(var3, 5)
var3 = add(2, 3)
print "%d apples." % total
In the Python version, we'll still get 8 apples. In naïve Rall:
def add(var1, var2):
return var1 + var2
add(1, 1)
var3 = add(1, 2)
total = add(var3, 5)
var3 = add(2, 3)
print "%d oranges." % total
Well, whoop-di-doo. That'll be 10 oranges for you. var3
is actually defined when print
is run, so it takes on the latter value. I said “naïve” above because I'm not sure this should be how it works. Perhaps Rall should:
- have versioning for the definition, so
total
depends onvar3.v1
, notvar3.v2
, and/or: - save the scope as it is before a definition a requested.
I'm not sure.