by-value vs by reference (was Re: list question)
by-value vs by reference (was Re: list question)
- Subject: by-value vs by reference (was Re: list question)
- From: Andrew <email@hidden>
- Date: Thu, 05 Jun 2003 18:14:22 -0400
Here's a quick re-cap of all the information that's been passed on in this
thread with some definitions.
Object's property:
Some variables can contain sub variables. Sub variables are referred to by
using the "of" keyword in applescript. So "the value of date" is accessing
the variable "value" of the object "date". Sub variables are called
"properties". So in the case above "value" is a property of "date".
Object:
An object usually refers to a variable that consists of other variables and
methods. An object can has no methods and an object can have no properties
or one property so in reality, depending on the language, everything could
be an object. Date objects are objects because they have properties. Numbers
like 1,2.3 etc.. in applescript might be objects behind the scenes or could
be viewed as either primitive data types passed by value or a special kind
of object called an immutable object (see below).
Primitive data type:
Some languages have a split between objects and primitive data types. In
Those languages, primitive data types are always passed and accessed by
value and objects are passed by reference. (java and javascript are
languages that make this split).
Immutable Object:
Is an object whose value(s) (aka properties) cannot be modified once it has
been created. This mean that all the sub values are constant and any sub
objects inside the immutable object must also be immutable (which means all
their properties must be constant etc..).
Mutable Object:
And object which has at least one value that can/could be be modified or for
that matter one sub value of one sub-sub value etc...
Accessing / passing by value:
Is a kind/concept way of dealing with variables. If you access a variable by
value it means that a variable represents a value forever or until that
variable is assigned another value. This means that
set y to 5 -- assigns 5 to y
set x to y + 1 -- assigns the RESULT of (y+1) to x
=> x is 6 y is 5.
y will contain 5 forever or until somebody assigns something else.
A reference:
Accessing a variable by reference means that your variable don't have a
value. instead they point to an implicit entity which contains the value...
So if Applescript accessed numbers by references then
set y to 5 -- creates a new number with the value of 5
set x to y -- sets X to the same number as y
set the value of y to 6 -- set the number to 6
return x
=> x refers to a number which has the value of 6.
Which mode of access you use depends on your language.
C uses by value with an option to create a dumb-as-dirt reference called a
pointer.
4d uses by value with an option to create references called pointers to
every data type except for convenient ones.
Java does by value for primitives and by ref for objects
Python, same sort of thing as java I believe...
Applescript same sort of thing as java... sort of.. maybe...
What does mutable / immutable have to do with accessing by reference or
accessing by value??? ->
Well, some time ago, someone discovered that if you're in a language that
accesses variables by references only... and if you're using object that
can't be modified after they are created then the way you would use these
object matches EXACTLY the way you would use them if you were in a language
that accessed variables by value... This discovery was met by jubilation and
much drinking.
Eg:
Let's say that we're in an imaginary Applescript that accesses primitives by
reference with code like this ->
set y to 5 -- creates a new number with the value of 5
set x to y -- sets X to the same number as y
set the value of y to 6 -- set the number to 6
return x
=> x refers to a number which has the value of 6.
Now let's say that there was no way of setting the value of y to 6 so this
line ->
set the value of y to 6 -- No allowed! Object cannot be modified
isn't allowed..
You could still add two number together and get the result but you need an
operator that created a *new* number object when two number were added
('cause there's no way of setting the value, so we just make a new one with
a different value.).. so that you could do this
set y to 5 -- creates a new number with the value of 5
set x to y -- sets X to the same number as y
set y to 6 -- creates a new number with the value of 6
return x
=> x is 5!
Notice that X is now 5.. That is because when we did this:
set y to 6
we created a *new* number object with the value 6! So now y doesn't point to
the number object with the value 5 it points to the number object with the
value 6, which is a different object entirely. (and has to be because
there's no way of modifying a number object's value once it's created)
same idea with this ->
set x to y + 1
(y + 1) creates a NEW object with the value of y + 1... IF y was not
immutable we could change the value of the newly created object by doing
something like
set the value of x to 9
but if we make the number object immutable then we can't do this so, as I've
mentioned many times by now, we just make a new number object one instead...
Notice that the behaviour of immutable objects in a language that accesses
variable by reference is the same as a language that accesses variables by
value.
In Java, which treats primitive data types (like number types) by value and
complex data types like all objects by reference, there is a way of telling
primitive data types from immutable objects. In Applescript, who really
knows if Applescript treats primitive data types by value and complex data
types by reference or is simply using immutable object for its primitive
data types? Since it's impossible to know, the whole question is somewhat
academic. All that's really important is the understanding that things like
numbers and strings are always copied when they are being modified (because
they are either being accessed by value or are immutable).
Anyway, the point of all this is that Applescript appears to access
everything by reference except primitive data types which may be by value or
maybe immutable objects. This means that object and lists and anything that
you access properties by doing this ->
set PROPERTY of OBJECT to SOMETHING
is going to be passed around by reference but that operators like (+, -, /,
*, &) will create a new brand new object/variable, in order to support "by
value" access to primitives.
NOTE : As a general style guideline it's a good idea to access variables "by
value"/use immutable objects since it minimises un-intentional side effects
on your code.. That is one change can't affect anything not in the scope or
not returned.
NOTE : The main downside to designing a language where everything is
accessed by value is if you have large variables you can waste allot of time
creating/copying these big objects around for minor changes. Also, if
immutables-via-references aren't used the variable is copied for every
function call wasting a great deal of memory for large objects.
NOTE : I've been reading the posts and it turns out you can actually
construct an explicit reference to a primitive data types and shuttle the
reference around. This means you can get back the functionality of the
set the value of x to 5
behaviour which can be handy if you're, say, returning values from a
function using the parameters as well as other things..
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.