View Full Version : Keeping References to Primitive Data Types/Properties?
IQAndreas
October 26th, 2009, 04:54 PM
I included a little recap. Skip down to the non-italisized text for the actual questions.
Primitives
Primitive Data Types (http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000047.html) (Boolean, int, Null, Number, String, uint, and void) are treated as "values" rather than Objects, so when you pass one of them into a function, instead of passing a reference, it basically clones the information, and creates a new instance. It does the same thing when when you use "primitive1 = primitive2", and instead of passing on a reference, clones it.
I thought there might be a slim possibility Flash allows me to reference primitives if I treat them as objects instead of (in this case) ints.
var num1:int = 5;
var num2:Object;
num2 = Object(num1);
num1 += 5;
trace(num1, num2);
//Outputs: 10 5
//FAILURE :(
Is there any possible way to create variables that contain references to primitive values instead of their actual values?
I could create a new class for each primitive which has one property, "value", which basically allows you to pass in that object's reference to a function, and that function can then change the value property. When you leave the value property, that class reference will still hold the changed value.
Sadly, it can't be passed into any functions that require that value, and it can't extend any primitive value because they are all final (curses!)
Properties (getter/setter methods)
Since getter/setters are technically functions, is it possible to contain references to them? Most Tweening engines have you pass in the object and property name as a string, which works, but I like being difficult. :fight:
Making Flash treat your classes as primitives
[MOVED TO NEW POST TO AVOID CLUTTER]
IQAndreas
October 26th, 2009, 04:54 PM
Final question (under the category of "likely not possible, but I'll give it a shot") is it possible to have more control over your classes by having Flash treat them as if they were primitives.
That means:
Every time you set an object equal to your object, it always clones it, or at least your class is told every time the actual object [reference] is trying to be accessed and not just it's properties.
When you pass it into any function, it is cloned
It is able to have a "default" value, so if that class is set to something you don't like, or not set to anything at all, instead of null, it is instead set to the default value.
Basically, that would mean controlling (or at least being notified) every time the actual variable (not object) is being accessed or changed, instead of just individual properties
var myPrimitive:PrimitiveClass; //PrimitiveClass is notified of this
myPrimitive = fantastic; //Primitive class is notified of this
myPrimitive = null; //Primitive class is notified of this
myFunction(myPrimitive); //Primitive class is notified of this
obj2 = myPrimitive; //Primitive class is notified of this
Am I making myself understandable of what I'm trying to accomplish?
I have half a mind to just write my own language, hijack a Flash compiler source and some open source flash player out there, and write my own programming language that is usable my my special Flash player (reverse compatible with Adobe's Flash).
I shall name it "AndreasScript 3.0" :thumb:< Putting the "ripped" (ript) back in "Script"
Krilnon
October 26th, 2009, 05:15 PM
For all of your "is it possible" (in ActionScript) questions: no.
I could create a new class for each primitive which has one property, "value", which basically allows you to pass in that object's reference to a function, and that function can then change the value property. When you leave the value property, that class reference will still hold the changed value.
Sadly, it can't be passed into any functions that require that value, and it can't extend any primitive value because they are all final (curses!)
That's basically what you'd have to do (or var primitiveWrap = {val: someVal}), and the resultant hack wouldn't be usable, because, as you noticed, you couldn't pass your wrapper into code that anyone else wrote expecting the non-wrapped form.
You can't hold references to "true" accessor methods (the ones that you declare with function set/get because any syntax that would let you access the reference is already used to call the accessor instead. You could have an un-"true" accessor that you implement through Proxy… but you can imagine the incompatibilities there.
I have half a mind to just write my own language, hijack a Flash compiler source and some open source flash player out there, and write my own programming language that is usable my my special Flash player (reverse compatible with Adobe's Flash).
I shall name it "AndreasScript 3.0" < Putting the "ripped" (ript) back in "Script"
You can already compile haXe, C, and C++ into stuff that kind of works with Flash Player. It's not that writing your own language and runtime wouldn't be fun or interesting, but it's not necessary to do all of that just to allow you to essentially store and manipulate pointers in your code that targets Flash Player.
TheCanadian
October 26th, 2009, 10:05 PM
That's pretty lame that primitive classes are final. Why the heck shouldn't we be allowed to extend these? In fact, the final attribute itself can just gtfo!
In AS2 it was sort of possible since there was no such thing as final classes. With the wonder that is Object.valueOf, some pretty functional classes were feasible. The only thing that would kind of bung you up was trying to use operators but even that was possible using a load of type casting.
CS5 needs expendable primitive data types and extendable operators.
wvxvw
October 27th, 2009, 03:49 AM
I think that making prime types into fool featured objects will affect the performance. However, even in AS3 you may make custom classes that may be treated as numbers (or could be Booleans or Strings - I didn't try that though, just never needed it). You will need to define valueOf() and toString() methods for them in a way they return numbers (or print numbers).
package org.wvxvws.utils
{
/**
* Double class.
* @author wvxvw
* @langVersion 3.0
* @playerVersion 9.0.115
*/
public class Double
{
//--------------------------------------------------------------------------
//
// Private properties
//
//--------------------------------------------------------------------------
private var _high:uint;
private var _low:uint;
//--------------------------------------------------------------------------
//
// Cunstructor
//
//--------------------------------------------------------------------------
public function Double(high:uint, low:uint)
{
super();
_high = high;
_low = low;
}
//--------------------------------------------------------------------------
//
// Public methods
//
//--------------------------------------------------------------------------
public function valueOf():Object
{
return _high * uint.MAX_VALUE + _low;
}
public function toString():String
{
return Number(this.valueOf()).toString();
}
}
}
package tests
{
//{ imports
import flash.display.Sprite;
import org.wvxvws.utils.Double;
//}
/**
* TestDouble class.
* @author wvxvw
* @langVersion 3.0
* @playerVersion $(playerVersion)
*/
public class TestDouble extends Sprite
{
//--------------------------------------------------------------------------
//
// Cunstructor
//
//--------------------------------------------------------------------------
public function TestDouble()
{
super();
var d:Double = new Double(uint.MAX_VALUE, uint.MAX_VALUE);
trace(d); //18446744069414584000
trace(Number(d).toString(16)); // ffffffff00000000
// Need casting :( compiler thinks that only numeric types are divisible
trace(Number(d) / 2); //9223372034707292000
d = new Double(0, 10);
// Compiler is OK with addition because it may be used for a bunch of other things
trace(d + 10); //20
}
}
}
Actually, I think you may have char datatype or Byte, or Short in this way too. However, you need to understand that those are going to be less performant than native types.
PS. Also per this example note the precision loss. You'll have to do the calculations differently to get the more precise double than that :)
IQAndreas
October 27th, 2009, 06:34 AM
Well, the class works. The only problem is you can't do simpler things like this:
d += 5;
In fact, you can't set d to anything without creating a new Double instance, but as long as the class is used for your own code and not passed into any other classes you didn't write, it should work just fine. Very nice, thank you. :)
I have definitely used toString before, and if I understand correctly, that is the function that is run every time the class is treated as a string (such as in trace statements), but what does the toValue function do? If it treats it as a numerical value, how does it know the difference between treating it as a number or a string when adding the class together with some other value?
Basically, treating primitives as instances of custom classes would be possible if Flash allowed you to "emulate" other classes (making other classes think you are passing in a certain class), as long as you have all the same properties. I'm still longing for that feature. It would be so simple as just allowing classes to be used as interfaces, "public class Double implements uint"
wvxvw
October 27th, 2009, 01:57 PM
Well, AS3 isn't Java or C# :) and, I'm not sure it needs to be. It's cool it has now all those modern things, but, at some point, you'd maybe start liking the simplicity of it as comparing to full-featured languages.
15.2.4.3 Object.prototype.valueOf()
As a rule, the valueOf method for an object simply returns the object; but if the object is a “wrapper”
for a host object, as may perhaps be created by the Object constructor (see section 15.2.2.1), then the
contained host object should be returned.
This is from ECMAScript docs.
So, if you want to "wrap" the numeric type, it would be the proper place to do that.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.