Var and Default

BlitzMax Forums/BlitzMax Programming/Var and Default

Tibit(Posted 2005) [#1]
Is there anyway I can use the command var with defaults, so I can leave out fields. Ex. Function Test(Var A = 0) The most simple way I could figure out was to do several functions. I for each number of variables.
It works if I do Apply(X,Y,Empty,Empty,Empty) but that doesn't just feel good.




Global Arr[]

Arr = [799,599,359,99]

Function Apply1( A Var )
	If Len(Arr) > 0 Then A = Arr[0] ;DebugLog "A Set"
EndFunction
Function Apply2( A Var , B Var )
	Apply1( A )
	If Len(Arr) > 1 Then B = Arr[1]
EndFunction
Function Apply3( A Var , B Var ,C Var )
	Apply2( A, B )
	If Len(Arr) > 2 Then C = Arr[2]
EndFunction
Function Apply4( A Var , B Var ,C Var , D Var)
	Apply3( A, B, C )
	If Len(Arr) > 3 Then D = Arr[3]
EndFunction
Function Apply5( A Var , B Var ,C Var , D Var, E Var)
	Apply4( A, B, C, D )
	If Len(Arr) > 4 Then D = Arr[4]
EndFunction


X = 800
Y = 600
Dir =360
Armor = 100

Print "X: "+X
Print "Y: "+Y
Print "Dir: "+Dir
Print "Armor: "+Armor

Apply1(X)

Apply2(X,Y)

Apply3(X,Y,Dir)

Print "X: "+X
Print "Y: "+Y
Print "Dir: "+Dir
Print "Armor: "+Armor



LarsG(Posted 2005) [#2]
I'm not sure what you're trying to do here, but have you considered making a list of types rather than a massive array?!


Perturbatio(Posted 2005) [#3]
you could pass by pointer:
Local x:Int = 8
Local y:Int = 90
test(x,Varptr y)

Function test(a:Int, b:Int Ptr = Null)
	Print a
	If b Then Print b[0]
End Function




Tibit(Posted 2005) [#4]
@LarsG - I'm not sure what you mean, massive?

@Perturbatio - Thanks that was a good solution but then people have to use Varptr infront of every variable. I guess I'll simply go it with a couple of functions as in my first post.


N(Posted 2005) [#5]
I'm not sure what you're trying to do here, but have you considered making a list of types rather than a massive array?!


Didn't realize four elements was massive...

Anyhow, I have no idea what you're trying to do there.


Scott Shaver(Posted 2005) [#6]
why would you want a default for that? The point of var is to allow you to return a result in a given parameter. If you don't need the result back then use normal parameter defaults:

function foo(a:Int, b:Int=0)

if you need the value back then having a default is pointless becuase you wouldn't have supplied a variable to store the value in.


Tibit(Posted 2005) [#7]
Let's say I want to set the start position of an object. So I make a function using var like this:
SetMidPos(X,Y)

function SetMidPos(W Var,H Var )
W = 200 ;H = 300
endfunction

Let's say I sometime want to use X,Y,Z

SetMidPos(X,Y,Z)
function SetMidPos(W Var, H Var, D Var)
W = 200 ;H = 300; D = 100
endfunction

If I want to use SetMidPos(X,Y) now it will not compile because it's missing the "D" parameter.

So I want something like this:
function SetMidPos(W Var, H Var, D Var = None)
W = 200 ;H = 300;
If D then D = 100
endfunction

Both SetMidPos(X,Y) or SetMidPos(X,Y,Z) should work.

In this way I can use the same function for both cases. This example is not related to my real code =)
It was just to show why it could be very handy especially with multiple Vars like D.

I could always go SetMidPos(X,Y,SomeTempVar) but that doesn't feel right.


Scott Shaver(Posted 2005) [#8]
Well I suppose you could it this way

function SetMidPos(W:Float, H:Float, D:Float = -9999)
if D!=-9999 then = 100
end function

assuming you don't want the values back. That example you use is really not a Setter it is a Getter. Perhaps that is where my confusion lays.

function GetMidPos(W:Float Var, H:Float Var, D:Float = None)
W=100; H=200;
if D then D = 100
end function

I can see the use as a getter in the above example. However it seems a bit like to much syntactic sugar to support when you can simply ignore the values you don't want back with the current way of doing it.

Eye of the beholder I suppose. :)


Tibit(Posted 2005) [#9]
I would use it to get and update my current vars with other values.

Update(X,Y,Dir,Armor) - Receive new values

I just like the idea of having the same function for

Update(Energy,Power)

or

Update(X,Y,XSpeed,YSpeed,XAcceleration,YAcceleration)

Would be a smooth interface for the end user (end programmer).


Dreamora(Posted 2005) [#10]
I somehow don't see the problem.
You need default values on Set, where you don't need var at least if the interface should be somewhere "usefull" for end programmers which search for "MiddlePos()" or "GetMiddlePos()" to get these values.

Beside that: A better distinction of different functions make stuff far better usable ie.
get_middle_pos_2d (W:float var, h:float var)
get_middle_pos_3d (w:float var, h:float var, d:float var)


I know, more work than C++ overloading but by far better distinction as well if you need different implementations later etc


Tibit(Posted 2005) [#11]
I don't really want overloading (and I don't know C++ but I guess overloading is basically the same as in Java), just a optional var. I mean think if you have an array with incomingdata[]

Instead of
X = IncomingData[0]
Y = IncomingData[1]

You could go ApplyIncomingData(X,Y)

'If you have a function like this:
function ApplyIncomingData(A var,B var)
A = IncomingData[0]
B = IncomingData[1]
endfunction

Think about that but with more than X,Y or with other things than just X,Y. I want the number of function parameters to be equal to the Length of the IncomingData Array =)

The only way I can think of doing this is to do it with pointers somehow. The problem is I have never worked with pointers, I don't even know if pointers can help me out here.