Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Parameters are the objects you can pass to Functions, Closures, or Callable Types which handle Events.

A parameter is declared with a name, followed by "as", followed by the type: paramname as type. If there is no "as type", then the type is assumed to be type object.


Method/Function example

Code Block
def mymethod(x as int, y as long):


Code Block
c = def(z):
         print z

obj = "a string"

Callable Type + Event

Code Block
import System

class Sandwich:
	event Eating as EatingEvent
	callable EatingEvent(sammich as object, type as string)

	def Eat():
		Eating(self, "Turkey sammich.")

turkeyAndSwiss = Sandwich()
turkeyAndSwiss.Eating += def(obj, sammich):
	print "You're eating a $sammich! It must be good."

Variable number of parameters

Boo allows you to call or declare methods that accept a variable (unknown) number of parameters.

You add an asterix (*) before the parameter name to signify that it holds multiple parameter values. If there is no 'as type', the type is assumed to be an array of objects: (object). You can declare the type as any array type. For example (int) if your method only accepts int parameters.

Here is an example:

Code Block
def mymethod(x as int, *rest):
	print "first arg:", x
	for item in rest:
		print "extra param:", item

mymethod(1, "a", "b", "c")
mymethod(2, 3, 4, 5, 6, 7)

Some boo builtins accept a variable number of parameters, like matrix() and ICallable.Call.

ByRef parameters

Add a "ref" keyword before the parameter name to make a parameter be passed by reference instead of by value. This allows you to change a variable's value outside of the context where it is being used. Some examples:

Basic byref example:

Code Block
def dobyref(ref x as int):
        x = 4

x = 1
print x //-->1
print x //-->4

DllImport example:

Wrapping a native method that takes a parameter by reference.

Code Block
import System.Windows.Forms from System.Windows.Forms
import System.Drawing from System.Drawing
import System.Runtime.InteropServices

class ExtTextBox(TextBox):
	//must be static
	static def GetCaretPos(ref p as Point):

f = Form(Text: "byref test")
t = ExtTextBox()

b = Button(Text: "GetCaretPos")
b.Click += do:
	p = Point(0,0)
b.Location = Point(0,100)

See also tests/testcases/integration/byref*.boo in the boo source distribution.