Forums » Discussions »
Shared vs. Separated Storages for Functions and Variables?
Added by Heinz Gies over 1 year ago
Okay here is a quite essential question:
Should functions and variables share one storages or use separated ones?
First I'd like to clear up definitions:
Shared storage
Both functions and variables reside in one storage, thus a function can be handled like a variable or the other way around. But there can't be a variable and a function with the same name.
Separated storage
There is one storage for functions and one for variables, they are completely separated and a function is really just a function.
The handling of storage here isn't meant to be what the language in the end behaves like but how the VM internally handles matters. I am sure that whatever way is chosen it is possible to have a compiler pretend to use shared or separated storages for it's language.
Both ways have their merits and flaws, question is which one allows it easier to simulate the other.
Replies
RE: Shared vs Sepperated sotrages for Functions or Variables.
-
Added by Heinz Gies over 1 year ago
Here a few idea:
Using namespaces to simulate sepperated storages
Implementing namespaces in the enviroment would allow to pretend sepperated storages while they are actually shared by storing functions in one namespace and variables in another.
Using automatic creatin of functions to sumulate joined storages
The FunctionCall could, in the case of not finding a function, look in the variables and see if it finds a variable with same name and a Block as value then automaticall creats a function that calls this block.
RE: Shared vs. Separated Storages for Functions and Variables?
-
Added by Kornelius Kalnbach over 1 year ago
Examples of separated versus shared storage in Ruby¶
Ruby has shared storage
...although it looks like separated at first ;)
- Ruby has no functions, but private methods of the main object behave like global functions.
- Ruby stores methods in the scope of classes.
- Methods defined on objects reside in the metaclass for this object.
- Local variables reside in the scope/closure they were defined in or where they are used/referenced.
- It makes sense for a pure-method language like Ruby.
Example
def fun(bar = nil); :method end fun = :local_variable fun # => :local_variable fun() # => :method fun 4 # => :method fun fun # => :method self.fun # => private method `fun' called for main:Object (NoMethodError)
Rules for telling local variables and self methods apart (Ruby)
foo.baris always a method call.bar()is always a method, even if bar looks like aCONSTANT.- There is no
with...doconstruct. - When looking at a
fooreference, Ruby checks the local variables of the scope:- If an ident is assigned, let's say
foo = 42, it defines a local variablefoowhich lives until the end of the block or class/method definition. - If Ruby sees
fooagain without a receiver (obj.foo) and without any arguments (foo(42),foo 42,foo()), it makes it reference the local variablefoo. - Otherwise, when it can't assign it to a local variable, it assumes it's a method, calling
self.fooin private scope.
- If an ident is assigned, let's say
- You can't use setters inside a method definition, because
a = balways assigns to a new variable. - Sometimes it gets confusing when you forget which local variables are defined in your block: What does
result = handlemean? - Worst of all, there's neither a simple way to get a method object (
foo.baris a call and does not return thebarmethod object; you have to usefoo.method :bar) nor a simple way to call a method object (foo()for a local variablefooresults in aNoMethodError.)
Bottom line: Since it is clear at compile time what is a local variable and what is a method call, you don't need separated storage to interpret Ruby.
RE: Shared vs. Separated Storages for Functions and Variables?
-
Added by Kornelius Kalnbach over 1 year ago
Examples of separated versus shared storage in JavaScript¶
Since I don't know ECMA well enough ;)
JavaScript has shared storage
- Functions are simply objects.
- Function objects are called by using
(). - When referencing a function object without calling it, you get a reference to the object.
- Calling a variable that is not a function results in an exception.
Example
fun = function() { return "method"; };
fun // some function object
fun() // "method"
fun = "local_variable"
fun // "local_variable"
fun() // error...
Rules for telling local variables and functions apart (JavaScript)
- Since there is no difference, this is not necessary.
Bottom line: JavaScript script is using shared scope.