A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: AS3 Number Constructor Bug?

  1. #1
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139

    AS3 Number Constructor Bug?

    someone please tell me what the hell is going on here, the Number type is the only one that fails here:

    in a new fla add these line:
    Code:
    var test2:Test2 = new Test2();
    var test1:Test1 = new Test1();
    Test1.as
    Code:
    package {
    	
    	public class Test1 {
    		
    		protected var var1:String;
    		protected var var2:Number;
    		protected var var3:int;
    		protected var var4:uint;
    		protected var var5:Boolean;
    		
    		public function Test1(){
    			
    			trace("TEST 1 CONSTRUCTOR", var1, var2, var3, var4, var5);
    		}
    	}
    }
    Test2.as
    Code:
    package {
    	
    	public class Test2 extends Test1 {
    		
    		public function Test2(){
    			
    			var1 = "hello";
    			var2 = 10;
    			var3 = 5;
    			var4 = 15;
    			var5 = true;
    			
    			trace("TEST 2 CONSTRUCTOR", var1, var2, var3, var4, var5);
    			
    			super();
    		}
    	}
    }
    traces out:

    TEST 2 CONSTRUCTOR hello 10 5 15 true
    TEST 1 CONSTRUCTOR hello NaN 5 15 true
    TEST 1 CONSTRUCTOR null NaN 0 0 false


    ????????

    why is Number the only one coming back as NaN. And funnily enough its the number type I need, figures.
    lather yourself up with soap - soap arcade

  2. #2
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    This is really more out of curiosity than anything else, but have you tried moving the super () call to the top of the Test2 constructor before you assign the vars?

    EDIT:
    Never mind, that would just trace the same thing as when you instantiated test one on its own.
    Last edited by kortex; 10-30-2007 at 07:30 AM. Reason: Would not do anything
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  3. #3
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    yeah, thats the whole point of this test, to assign a var before calling the super constructor. I find it weird that the other types work and Number doesn't
    lather yourself up with soap - soap arcade

  4. #4
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    In order to get it to work, I had to :

    package {

    public class Test2 extends Test1 {

    public function Test2(){

    this.var1 = "hello";
    this.var2 = new Number(10);
    this.var3 = 5;
    this.var4 = 15;
    this.var5 = true;

    trace("TEST 2 CONSTRUCTOR", var1, var2, var3, var4, var5);

    super();
    }
    }


    }

    Which output:
    TEST 1 CONSTRUCTOR null NaN 0 0 false
    TEST 2 CONSTRUCTOR hello 10 5 15 true
    TEST 1 CONSTRUCTOR hello NaN 5 15 true

    But that does not seem to be setting it in Test1. Thats odd.
    Last edited by kortex; 10-30-2007 at 08:35 AM. Reason: adding trace results
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  5. #5
    Senior Member
    Join Date
    Jan 2006
    Location
    USA
    Posts
    383
    Why would you want to assign a var before calling the super constructor?

    You couldn't assign a var before calling the constructor when instantiating an object for Test1. Why would you want to do it for an object that extends it?

  6. #6
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    If you think you've found a valid bug, please report it
    http://www.adobe.com/go/wish

    linking to clean, clear, testable files would be a big help

  7. #7
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    Quote Originally Posted by AfternoonDelite
    Why would you want to assign a var before calling the super constructor?

    You couldn't assign a var before calling the constructor when instantiating an object for Test1. Why would you want to do it for an object that extends it?
    Interesting theoretical point, but the odd thing here is that all of the other types work and are assigned regardless of the fact the the super constructor has not (explicity) been called yet. It is only the number object that seems to have an issue.

    Additionally, you do not have to explicitly call the super constructor. Due to constructor chaining, it gets called anyway. So even if you comment out super() in Test2:
    package {

    public class Test2 extends Test1 {

    public function Test2(){

    this.var1 = "hello";
    this.var2 += 10;
    this.var3 = 5;
    this.var4 = 15;
    this.var5 = true;

    trace("TEST 2 CONSTRUCTOR", var1, var2, var3, var4, var5);

    //super();
    }
    }


    }

    You still get:

    TEST 1 CONSTRUCTOR null NaN 0 0 false
    TEST 1 CONSTRUCTOR null NaN 0 0 false
    TEST 2 CONSTRUCTOR hello NaN 5 15 true
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  8. #8
    Senior Member
    Join Date
    Jan 2006
    Location
    USA
    Posts
    383
    I wouldn't really consider it a bug, though. Maybe even more of a bug that the others worked.

    I don't think the variables should be initialized before the super constructor. All variables that need to be initialized to the super class should be passed to the super constructor, and if not, then initialized after the call to the super constructor.

    The super constructor should be the first thing called inside the constructor. Notice how in your last post that "Test 2" was the last thing traced out. That's because the compiler automatically called the super constructor before executing any code in Test2's constructor.

  9. #9
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    Yes, but my point was that the super constructor is called by default (hence the order you pointed out) and part of the point of inheritance is that you should not have to pass those variables and call the super constructor. I think we may actually agree on this, but are saying it in different ways.

    Regardless, you should be able to initialize inherited variables, otherwise, whats the point of inheriting them if you are going to initialize them by passing parameters to a the parent constructor? And, all of the other types are working as expected. Only number is not.
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  10. #10
    Senior Member
    Join Date
    Jan 2006
    Location
    USA
    Posts
    383
    I don't think I'm understanding
    part of the point of inheritance is that you should not have to pass those variables and call the super constructor
    The only real point of inheritance, that I understand, is to reduce code -- lack of repetition.

    Why would you need to initialize a parent's variable that the parent does not need to initialize itself?

    And if you do, you can initialize it after the parent's constructor. There shouldn't be a need to do it before. In fact, I'm surprised that it's not more tightly bound. I believe in Java, you must call the super constructor first if you are going to call it at all. And going from that, what would be the point of calling the super constructor without any parameters?

  11. #11
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    if Adobe had not changed or allowed the call to a super constructor in AS3 to be anywhere, then yes I would pass variables to the super constructor like in AS2. But they did, and so I wanted to try it out. On my first attempt I got this error.

    My real problem was like this: (consider a two class setup)
    Code:
    BaseClass {
    
    var SET_ONCE:Number;
    
    var can_be_changed:Number;
    
    public function BaseClass(){
    
    setVars()
    }
    
    private function setVars(){
    
    can_be_changed = SET_ONCE;
    }
    
    }
    Code:
    SubClass extends BaseClass {
    
    public function SubClass(){
    
    SET_ONCE = 10;
    
    super()
    }
    
    }
    you can see how this would not work, of course if you added it in the super constructors arguments, then yes it would.

    Its not really a problem, because there is a solution, more of a why?

    senocular, I guess I'll report it. Oh and here's a zip file with the bug:
    Last edited by mr_malee; 11-13-2007 at 05:38 AM.
    lather yourself up with soap - soap arcade

  12. #12
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    Yeah I think we basically agree, but are describing it in different ways.
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  13. #13
    Senior Member
    Join Date
    Jan 2006
    Location
    USA
    Posts
    383
    Yeah, I understand that you're trying to set a variable that the super constructor will use. But theoretically, that's only misusing inheritance.. and therefore, it's not a 'bug'.

    The best solution would to either have your constructor something like

    Code:
    public function Test1(num:Number=null) {
       ...
    }
    Otherwise, why are you calling that method in your super constructor?

    Every time that you'd create the Base class's object, you're calling a method you don't need because you'll never set the variable before you call that method.

    So, my suggestion is to either allow the variable to be passed. If not, it doesn't seem like you need a base class.

  14. #14
    Junior Member
    Join Date
    Nov 2007
    Posts
    1
    Hey all.

    I came across this anomaly today.

    Has anyone found out why this is happening yet? Is it a bug?

    Also, what workarounds are you guys using?

    This is what I'm doing, but it seems a little inelegant (because I have to repeat the "_init();" code in all my subclasses):
    Code:
    package
    {
    	public class MyBaseClass
    	{
    		private var _myVarA:Number;
    		protected var _myVarB:Number;
    		private var _myVarC:Number;
    		
    		public function MyBaseClass()
    		{
    			_myVarA = 400.5;
    		}
    		
    		protected function _init():void
    		{
    			_myVarC = _myVarA*_myVarB;
    		}
    	}
    	
    	public class MyClass extends MyBaseClass
    	{
    		public function MyClass()
    		{
    			super();
    			_myVarB = 25.5;
    			_init();
    		}
    	}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center