How about auto int/float -> int array/float array?

BlitzMax Forums/BlitzMax Programming/How about auto int/float -> int array/float array?

skn3(Posted 2006) [#1]
I just came upon a situation where this would be useful. So how about auto conversion rules from int/bye/etc into single dimension arrays of said type ?
Function test(array:Float[])
	Notify array[0]
End Function

Local array:Float[] = [666.999]
test(10.123)
test(array)

... you could do this.

Currently youd have to do this instead..
Function test(array:Float[])
	Notify array[0]
End Function

Local array:Float[] = [666.999]
test([10.123])
test(array)


... incase in [ ].

Not really that important just an idea.

It would mean you can have a function that accepts an array or a single value. (without having to add the [ and ])

Below is an example similar to what made me think about this.
Type Tanimation
	Field image:timage
	Field frames
	Field delays:Int[]
	
	Method SetAnimationFrameDelays(ndelays:Int[])
		Local slot:Int
		If ndelays.length = 1
			'only 1 frame delay is passed, apply same delay to every frame
			For slot = 0 Until delays.length
				delays[slot] = ndelays[0]
			Next
		Else
			'an array o delays passed, each frame has unique delay time
			delays = ndelays
		End If
	End Method
End Type

Local a:tanimation = New tanimation
a.SetAnimationFrameDelays(20)
a.SetAnimationFrameDelays([20,20,20,1000,20])


Function overloading would probably be a better way to do this anyways...


H&K(Posted 2006) [#2]
Tell you what, why do we need to define what type of variable any paramater is. Why cannot I do
Function a:Int(P:Any)
and then it be my job to internaly deal with all the types that I might want to pass to the function.


ozak(Posted 2006) [#3]
Yup. A bit like somefunc(char* fmt, ...) in c, where you parse the parameters yourself. Would be usefull, but is not all that needed since functions can have overloads and default parms.


Jim Teeuwen(Posted 2006) [#4]
Personally I would rather have the type-safety of an explicit type definition. The times I use a char* or even a void* in c++ is rare at best. It requires you to make assumptions about the data that is being passed to it and one helluva lot of error checking. All extra overhead that can be prevented by simply overloading a function for a number of times.

It may be considered safe if you can be absolutely sure that the data passed to it is in a specific format. Making it a publicly accessible funciton (as part ofa library) can cause all manner of headache's for the ppl who don't know exactly what the function is for and what it does internally.

As for skn3[ac]'s suggestion.. it seems trivial really.
Besides, how would your function deal with this:


function foo( a:float[], b:int, c:float = 0)
 '// blah
end function

test(123, 321, 1232, 122);


how will the compiler know which parameters belong to the array and which don't?


skn3(Posted 2006) [#5]
Oh well maybe my post wasnt that clear. I just meant a conversion for a single float value into a single dimension float array, should you pass a float when a float array was expected.

Not really that important just an idea.


Being able to parse all the variables would indeed be good tho!


H&K(Posted 2006) [#6]
@Jim,

Dont get me wrong, I love the implicite nature of paramaters ATM, but without full overloading, the next best is base "Object" passing, which Im quite able of doing. I just think that for the base types (Int, float), which are not extended from "Object", it would be nice if I could have a :BaseAny parameter.


grable(Posted 2006) [#7]
a Variant datatype would be nice to have, especialy for mixed arrays (we could finaly get a printf() like function ;)