Constructors in Turing 
	 
	
		| Author | 
		Message | 
	 
		 
		uncompetence
 
 
 
    
		 | 
		
		
			
				  Posted: Tue Jun 09, 2009 10:58 pm    Post subject: Constructors in Turing  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				| I'm toying with Object-Oriented in Turing and looked through the reference under 'class' and found no means of writing a constructor for an object. All calls to new perform the initialization of the object, but there seems to be no way of passing in variables. Is there no other way than to write and call a method of my own? | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
		 
		Sponsor Sponsor 
		 
  
		 | 
		
 | 
	 
	 
		  | 
	 
				 
		wtd
 
 
 
    
		 | 
		
		
			
				  Posted: Tue Jun 09, 2009 11:44 pm    Post subject: RE:Constructors in Turing  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				| As far as I know, there is no way to have a constructor in the sense that other object-oriented programming languages have. | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
				 
		[Gandalf]
 
  
 
    
		 | 
		
		
			
				  Posted: Wed Jun 10, 2009 1:49 am    Post subject: RE:Constructors in Turing  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				| Indeed, you are stuck with creating your own initialize() function and always calling that. | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
				 
		DemonWasp
 
 
 
    
		 | 
		
		
			
				  Posted: Wed Jun 10, 2009 8:03 am    Post subject: RE:Constructors in Turing  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				| Similarly, there's no way to have a proper destructor. | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
				 
		Tyr_God_Of_War
 
 
 
    
		 | 
		
		
			
				  Posted: Thu Jun 18, 2009 7:03 pm    Post subject: Re: Constructors in Turing  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				I do not think that you can have any class methods (also know as static methods) at all.  That's why I think it's better to use a type and a module.  You can treat it like python, where you have to list 'self' as a parameter for your methods.  
 
	  | Turing: | 	 		  
module Boxes
     export unqualified opaque /* if you want the boxes to be only moddified inside the class*/ Box, draw, create
     type Box  :
        record
            left, bottom, right, top  : int
        end record
    fcn create  (l, b, r, t  : int) : Box
         var temp  : Box
 
        temp.left  := l
 
        temp.bottom  := b
 
        temp.right  := r
 
        temp.top  := t
         result temp
     end create
     proc draw  (b  : Box, col  : int)
        Draw.Box (b.left, b.bottom, b.right, b.top, col )
    end draw
     %.......................
end Boxes
 var myBox  : Box  := Boxes.create  (20,  40,  30,  50)
Boxes.draw  (myBox,  black)
  | 	  
 
Compare this to a class version.
 
	  | Turing: | 	 		  
class Box
     export left, bottom, right, top,
 
        create, draw  % and so on
    var left, bottom, right, top  : int
    proc create  (l, b, r, t  : int)
        left  := l
 
        bottom:=b
 
        right:=r
 
        top:=t        
     end create
     proc draw  (col  : int)
        Draw.Box (left, bottom, right, top, col )
    end draw
     % ect...
end Box
 var myBox  : ^Box
 new Box, myBox
 
Box  (myBox ).create  (20,  40,  30,  50)
Box  (myBox ).draw  (black)
free myBox
   | 	  
 
 
Or if you really want dynamically allocated stuff:
 
	  | Turing: | 	 		  
module Boxes
     export ~.Box, draw,  create, delete
     type Box  :
        record
            left, bottom, right, top  : int
        end record
    fcn create  (l, b, r, t  : int) : ^Box
         var temp  : ^Box
         new temp
 
        ^temp.left  := l
 
        ^temp.bottom  := b
 
        ^temp.right  := r
 
        ^temp.top  := t
         result temp
     end create
     proc draw  (b  : ^Box, col  : int)
        Draw.Box ( ^b.left, ^b.bottom, ^b.right, ^b.top, col )
    end draw
    
     proc delete (var b:^Box )
        free b
     end delete
 end Boxes
 var boxPtr:^Box:=Boxes.create (20, 40, 30, 50)
put "Left =  ",^boxPtr.left
 
Boxes.draw (boxPtr,  blue)
Boxes.delete (boxPtr )
  | 	  
 
 
As you can see, using a module and a type is much like using a class.
 
Modules can have static methods, while classes get inheritence and polymorphism. | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
				 
		 | 
	 
 
	
	
	 
	
	 |