Help Contents / Procedure Definition / be

bebe procname :input1 :input2 ...				(special form) 

Command. Like TO it prepares the definition of a procedure, but unlike TO, the variables scope is lexical. Also BE can be nested,  another BE definiton inside a BE definition is possible. 

Example: 

	be test1 :a :b 
		print :a 
		print :a+:b 
	end 
	test1 2 3 
	;2 
	;5 
	be test2 :a :b 
		be adder :a :b 
			output :a+:b 
		end 
		(print :a :b) 
		print adder :a :b 
	end 
	test2 2 3 
	;2 3 
	;5 

Additionally it is possible to use a procedure defined with BE as constructor for an Object. To do this, create a procedure, which creates some local variables in its main body, and additionally defines some sub-procedures,  also called member functions. 

Example: 

	be testobj3 
		be myNumber :a 
			be add :b 
				ifelse Number? :b  
				[	tmp=sum :a :b 
				][	tmp=sum :a :b'a 
				] 
				run :expr1 
				(type "\+) 
				run :expr2 
				output myNumber :tmp 
			end 
			local [expr1 expr2] 
			expr1=[(type :a)] 
			expr2=[(type ifelse Number? :b [:b][:b'a] "\= :tmp) (print)] 
		end 
		a=myNumber 2 
		b=myNumber 5 
		ignore (((a'add 3)'add 4)'add (b'add 6)) 
	end 
	testobj3 

will output: 

	2+3=5 
	5+4=9 
	5+6=11 
	9+11=20 

One class can inherit properties from one or more base classes by using the command INHERIT inside the constructor. 

All method function calls are done "virtual", that means if a class has a method ADD,  and a derived class has also a method ADD, the method of the derived class is called, also inside member functions of the base class. Non-virtual calls can be made explicitly, by using the namespace resolving operator :: like in C++. Preceding a function call with :: means global namespace. 

Example: 

	be testobj 
		::p=Point 3 4 
		::p2=Point 6 7 
		(show p'x p'y) 
		(show p2'x p2'y) 
		show (p'distance p2) 
		::m=Matrix_ p p2 
		(m'add m) 
		m'print 
		show m'det 
		show (m'x)'x 
		p'hallo 
		p'greet 
		p2'hallo 
		p2'greet 
		m'hallo 
		(m'x)'hallo 
	end 
	 
	be Halloer mytype 
		be hallo 
			(type [Hallo, I am\ ] mytype "\  string [.]) 
			(print) 
		end 
	end 
	 
	be Greeter mytype 
		be greet 
			(type [Greetings from a\ ] mytype [!]) 
			(print) 
		end 
	end 
	 
	be Point x y 
		inherit Halloer "Point 
		inherit Greeter "Point 
		be distance b 
			output sqrt (sqr x-b'x)+(sqr y-b'y) 
		end 
		be incx 
			x=x+1 
		end 
		be add p 
			x=x+p'x 
			y=y+p'y 
		end 
		be print 
			::print list x y 
		end 
		be string 
			output list x y 
		end 
	end 
	 
	be Matrix_ x y 
		inherit Halloer "Matrix_ 
		be det 
			output x'x*y'y-x'y*y'x 
		end 
		be add m 
			(x'add m'x) 
			(y'add m'y) 
		end 
		be print 
			x'print 
			y'print 
			::print Point::string 
		end 
		be string 
			output list x'string y'string 
		end 
	end 

will output: 

	3 4 
	6 7 
	4.24264 
	6 8 
	12 14 
		[6 8] 
		[12 14] 
	 
	-12 
	6 
	Hallo, I am point 6 8. 
	Greetings from a point! 
	Hallo, I am point 12 14. 
	Greetings from a point! 
	Hallo, I am matrix_ 	[6 8] 
		[12 14] 
	. 
	Hallo, I am point 6 8. 


previous: to	next: copyDef
