[haXe] Class Constructors and Return Types

James Jackson james at vdev.co.uk
Wed Jan 3 12:55:39 CET 2007


Hi Nicolas, 

I am just getting into HaXe, which by the way Nicolas I think is fantastic
:).  And I have notice something rather strange I wanted to check to 

see if this was has been missed by you when you wrote the complier as a
possible error.  If you create a class in haXe for example: let 

us create a television class and a Application test harness class: 

 

 

 

class Application{

            

     static function main(){

             var myTelevision:Television = new Television();     

            trace("active: " + myTelevision.active);

            trace("channel: " + myTelevision.channel);

     }

 

}

 

 class Television {

    

     public var channel : Int;

     public var active : Bool;

 

     public function new():Void{

            channel = 1;

            active = false;     

     }

 

}

 

When this class is compiled it return’s no error’s and if we trace out the
channel field we get 1 and the same if we trace out the active field we get
false; 

Now if give the constructor method a return type like a Boolean as below:




class Television {

    

     public var channel : Int;

     public var active : Bool;

 

     public function new():Bool{

            channel = 1;

            active = false;     

     }

 

}

 

The HaXe complier will output the following error:  “Application.hx:17:
lines 17-20 : Bool should be Void”.  Which is correct as we have not
specified a 

return statement; and that with constructors as far as I am aware are not
allowed to return anything. But if we actually add a return statement to the


constructor method like below: 

 

 

class Television {

    

     public var channel : Int;

     public var active : Bool;

 

     public function new():Bool{

            channel = 1;

            active = false;  

            return active;   

     }

 

}

 

It is simply ignored and does not cause a compile time error, I know it is
not urgent, and I want to check that you think the same as me and that it
should 

cause an compile time error as in ActionScript which errors as: “A
constructor may not return a value.”

 

Your thoughts would be most appreciated :) 

 

James 

 

 

 

 

 

 


-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.432 / Virus Database: 268.16.3/614 - Release Date: 02/01/2007
14:58
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.motion-twin.com/pipermail/haxe/attachments/20070103/37cd42d1/attachment.htm


More information about the Haxe mailing list