Variable conversion

BlitzMax Forums/BlitzMax Programming/Variable conversion

Henri(Posted 2014) [#1]
Did not know that you can pass different variable type as parameter to a function than what is expected and Blitmax does automatic type conversion.

For example this works:
Local d:Double = 32.6666666

Print Round(d)

Function Round:Int(f:Float)
	Return Int(f + 0.5)
EndFunction


-Henri


Derron(Posted 2014) [#2]
This is even more interesting:

local images:TImage[5]
local sounds:TSound[5]

Function CountElements:int(objects:object[])
   return objects.length
End Function

print CountElements(images + sounds)



@your code: this just works for numbers - or extending objects. So you cannot pass a string to your "Round()".


bye
Ron


Henri(Posted 2014) [#3]
With objects this is somehow expected as every object is first and foremost an object before given a special blueprint. (<--easy thing badly explained :-))

-Henri


Derron(Posted 2014) [#4]
So you knew that you could "accumulate" arrays ?

I just used it to add to arrays without knowing it (having read that approach some time before):

myarray :+ [entry]

instead of the previous

myarray = myarray[..myarray.length+1]
myarray[myarray.length-1] = entry


For me it always was clear, that "objects" could do things together because of their origin ("object") but never knew about that summing-up-thing. I told that Brucey 1-2 weeks ago (because of a bug in BCC-NG) and he thought that feature was added in one of the later versions of BlitzMax.


bye
Ron


Brucey(Posted 2014) [#5]
... he thought that feature was added in one of the later versions of BlitzMax.

Version 1.26, it seems.


Henri(Posted 2014) [#6]
Ahh, I see. Blitzmax is creating an array local to function with all the elements from both arrays. No, did not know this. Is there no end to the power of BM ?

-Henri


TomToad(Posted 2014) [#7]
+ will concatenate two arrays. So images+sounds will cause a new internal array of length 10 to be created and images and sounds will be copied to the new array after being cast to type Object. it will take half the time if you prevent the concatenation by passing only one array at a time.

SuperStrict

Local images:TImage[5]
Local sounds:TSound[5]
Local i:Int, t:Int
Local start:Int, time:Int

Function CountElements:Int(objects:Object[])
   Return objects.length
End Function
Delay(1000)

start = MilliSecs()
For i = 1 To 100000000
	t = CountElements(images + sounds)
Next
time = MilliSecs() - start
Print "Concatenate = "+time

start = MilliSecs()
For i = 1 To 100000000
	 t = CountElements(images)+CountElements(sounds)
Next
time = MilliSecs() - start
Print "Single Arrays = "+time



H&K(Posted 2014) [#8]
he thought that feature was added in one of the later versions of BlitzMax.
Every release after after the loss of Flushmem is a latter release to me. (lol)
Seriously 1.18 I consider "current", where we couldn't cast objects to ptrs (I think)


Derron(Posted 2014) [#9]
@TomToad

of course it is faster to just do arr.length + arrB.length. That concatenating could be used for more (eg. add various elements to a resourc array).

So the "counting" was just to show of what Blitzmax is capable of.

A better example is a scenario with some arrays getting appended to a collection array.


bye
Ron