Passing a type to a function

Blitz3D Forums/Blitz3D Beginners Area/Passing a type to a function

Farflame(Posted 2005) [#1]
Is this the correct way to pass a type to a function?

MyFunction(Alien)


Function MyFunction(Alien.Aliens)
Alien\X=1
Alien\Y=2
end function


Is that right?


Luke.H(Posted 2005) [#2]
Looks good to me, does it work?


Yan(Posted 2005) [#3]
Oops...Not BMax :oD


OJay(Posted 2005) [#4]
note, that you should always check, if the specified type-entry really exists, or else you will get a mav when accessing it.

Function MyFunction(Alien.Aliens)
  If Alien = Null Then Return
  Alien\X=1
  Alien\Y=2
end function



big10p(Posted 2005) [#5]
Sometimes it best not to check for Null and get a MAV. At least then (during development) you're informed of doing something wrong i.e. a bug. If you just let it silently fail, you could have a real nightmare on your hands when the 'silent bug' manifests itself somewhere else in your code.


OJay(Posted 2005) [#6]
its the coders turn to react to this "silent" fail, ie the function returns true on success and false on failure. then you would just check
if not MyFunction(myalien) then runtimeerror "MyFunction failed on checking 'myalien'"


etc etc...

trust me, every behavior is better than just throwing a MAV into the endusers face! :)


Farflame(Posted 2005) [#7]
Yes, the code does work, but I also noticed that calling the function with

MyFunction(Alien.Aliens)

works aswell. I suppose what I should have asked is, which one should I use, or is there a difference between MyFunction(Alien) and MyFunction (Alien.Aliens)...... ?


OJay(Posted 2005) [#8]
this makes absolutly no difference! as long as the alien type was created before the call.

its the same thing like other variables:

AnotherFunction(foo) is exactly the same as AnotherFuntion(foo%)
and
AnotherFunction(foo) is exactly the same as AnotherFuntion(foo#)
and
AnotherFunction(foo) is exactly the same as AnotherFuntion(foo$)

as long as foo got initialized in the right way before the function call (either as local or global...)!


big10p(Posted 2005) [#9]
trust me, every behavior is better than just throwing a MAV into the endusers face! :)
I'm not talking about end users. As I said, "during development". FarFlame's function makes changes to the fields of a type - if the code is passing a Null pointer to it at any stage, this is a bug in the code and needs to be addressed.

There are other situations where checking against Null is required, I'm just saying that this particular situation may not be wise to employ it. :)


octothorpe(Posted 2005) [#10]
Function MyFunction(Alien.Aliens)
  If Alien = Null Then RuntimeError("Alien = Null!")
  Alien\X=1
  Alien\Y=2
end function



WolRon(Posted 2005) [#11]
I suppose what I should have asked is, which one should I use, or is there a difference between MyFunction(Alien) and MyFunction (Alien.Aliens)...... ?


My Types tutorial should help explain it:

http://home.cmit.net/rwolbeck/programmingtutorial/reference/types.htm

excerpt from it:

A quick note about the variable 'thisStudent.Student'. Why do I have to place '.Student' after the variable name when creating a new instance of it?

The reason is because Blitz needs to know what kind of variable 'thisStudent' is. It is exactly the same behavior when we create any other new variables in Blitz. Every time we create a new variable we must let Blitz know what kind of variable it is by assigning an identifier to it.
If it's an integer then we need to assign a % to it (or nothing at all). If it's a string then we need to assign a $ to it. If it's a floating point variable, then we need to assign a # to it.

So, keeping with that behavior, if it's a Type, then we need to assign a Type identifier to it (in this case '.Student').

Having said that; since you only need to let Blitz know what kind of variable a variable is the FIRST time you use that variable (for example, mystring$ can later be written in your code as just mystring), the same is true for Types. The first time we use the variable 'thisStudent' we have to include the identifier '.Student' but every time thereafter, we only need to use the variable name 'thisStudent'.

First time we use the variable 'thisStudent' in our code:

thisStudent.Student = New Student
Every time thereafter:

thisStudent = New Student
Makes sense? If it doesn't, don't worry... You can always still use the type identifier with the variable. In other words, if you didn't understand this explanation, then just continue to write/use all of your Types like this:

thisStudent.Student = New Student