Fun With Types

Blitz3D Forums/Blitz3D Programming/Fun With Types

_PJ_(Posted 2008) [#1]
Ive had a quick search around, Im sure this must have cropped up before, but couldnt find anything.

I hope I can explain what I'm after, sorry if the words I use arent correct, I never know the 'proper' techy terms for things.

I am trying to return the Pointer to a Type.
Not an individual Field of a particular Type instance, but a means of identifying the instance itself without having to check for matching fields. (Because that has already been done)

As an example, consider:

Type Movies
Field Title$
Field Runtime
End Type


This simple Type would be populated with:
"Star Wars",110 
"Conan The Barbarian",90
"Big Trouble In Little China",87


All good so far, right?
Now, because of the amount of times where I need to iterate through the Type to find the instance I want, I want to put in a function like:

Function GetMovie(Title$)
	For IterateMovies.Movies=Each Movies
		If Iter\Title$=Title$
			Return IterateMovies	
			Exit
		End If
	Next
	Return Null
End Function


The above gives an "Illegal Type conversion" error at the line:
Return IterateMovies


Unsuprisingly because I just used that as an example, but as for what to actually substitute in is what I'm stuck on!

Is there any way to return the Instance itself from a function?


Gabriel(Posted 2008) [#2]
Yep, Blitz3D's unorthodox syntax for types makes things a little confusing here, but the answer is that you need to specify that you're going to return a type in the function prototype.

Try this:

Function GetMovie.movies(Title$)
	For IterateMovies.Movies=Each Movies
		If IterateMovies\Title$=Title$
			Return IterateMovies	
			Exit
		End If
	Next
	Return Null
End Function




_PJ_(Posted 2008) [#3]
Thanks a lot, that works fine!



Just for curiosities sake I'm gonna post this weird finding too!
As I was figuring how to then call the function, All the following compiled and ran fine:

First, this seems to be the 'correct' syntax to use:
InstanceName.Movies=GetMovie("Shaun Of The Dead")


My initial attempt, which I am unsure if it makes a difference, but this worked too:
InstanceName.Movies=GetMovie.Movies("Shaun Of The Dead")


And now, the really weird one:
InstanceName.Movies=GetMovie.SomeTypeNameThatDoesntEvenExist("Shaun Of The Dead")


Seems the actual function call ignores the . and everything between it and the ()

Although the following didn't work, so the compiler is clearly looking ONLY at the name of the handle.
InstanceName.SomeTypeThatDoesntExist=GetMovie.Movies("Shaun Of The Dead")



Gabriel(Posted 2008) [#4]
Indeed, that is somewhat odd. It's not dangerous because it's not possible to have two functions with the same name, but it is somewhat inconsistent as you can't do the same thing with function parameters. I guess you're right that the compiler just never looks to see what you've put there.