Forward declaration

BlitzMax Forums/BlitzMax Beginners Area/Forward declaration

Ibmurai(Posted 2005) [#1]
Is there no way to make forward declaration in BlitzMax?

i.e., something like:

'BEGIN CODE:
Type thingy
Function dothis(that, what)
Function dothat(hut, hat)
End Type

Function thingy.dothis(that, what)
' Some code
End Function

Function thingy.dothat(hut, hat)
' Some other code
End Function
'END CODE

I'm asking because I run into problems with the order of my functions descriptions. Function A is declared before function B and function A uses function B, so I get an error...

(Oh, and where can I find the specifications for the codes used in this forum for inserting screenshots and code boxes and stuff?)


JazzieB(Posted 2005) [#2]
You can't define functions within Types like that. Your example should be...

Type thingy
  Function dothis(that,what)
    ' some code
  End Function

  Function dothat(hut,hat)
    ' some other code
  End Function
End Type


You then call them with thingy.dothis(1,2), etc. The general order of things doesn't really matter (I think), as BlitzMax uses a multi-pass compiler.

As for the forum codes, I don't know where they are, but here are the ones used most (replace {} with []).

{code}
Your code
{/code}

{img}http://www.image_url.com{/img} - ignore dodgy link!

{a http://www.url.com}some text for your link{/a} - ignore dodgy link!

{b}bold text{/b}

Hope that helps.


FlameDuck(Posted 2005) [#3]
I think the keyword you're looking for is Abstract.

But Jazz is right, you can't define functions like in your example, and the order in which functions are declared is not important.
Type someType

	Function doThis()
		doThat
	EndFunction

	Function doThat()
		Print "Done that"
	EndFunction

EndType

someType.doThis



Ibmurai(Posted 2005) [#4]
To both of you: I know that BlitzMax wants the function definitions inside the type declarations - I just wish there was a way to forward declare functions like you do in C. But I guess BlitzMax just can't do that :(


FlameDuck(Posted 2005) [#5]
But I guess BlitzMax just can't do that :(
Which part of "I think the keyword you're looking for is Abstract" don't you understand?

But it's nonsensical in BlitzMAX unless you're dealing with polymorphism, because in BlitzMAX (as opposed to C, which uses a single-pass compiler) "the order in which functions are declared is not important".


Ibmurai(Posted 2005) [#6]
I think I'll drop my original question (since it's answered now :) thankyou all).

But I have a related question - What am I doing wrong here:
Type THouse
  Function Create()
    Self.MakeRoof
  End Function

  Method MakeRoof()
    'Some code
  End Method
End Type

The error I get is when calling Self.MakeRoof; "Identifier 'MakeRoof' not found".


Dreamora(Posted 2005) [#7]
Function is on Typebase
But method is on Instancebase.

you need to do

local t:THouse = new THouse
t.MakeRoof
return t


Ibmurai(Posted 2005) [#8]
AH! I see! So if I make MakeRoof a function instead, it should just work... Trying that... Yep it worked!

Thanks everybody!


Ibmurai(Posted 2005) [#9]
Man, this is annoying... BlitzMax is so strict with Fields and methods being instancebased and functions and globals being typebased, so I can't make completely self-contained objects which, for example, initialize themselves upon creation?

Or maybe I'm going about this the wrong way?

Maybe I shouldn't use the Create function but the New method instead... Would that work...? I guess I'm just typing while thinking here - I can't be the only one with this problem...


Dreamora(Posted 2005) [#10]
For creation functionality without parameters, you can use new and override method new () with you own initialisation.

For a create with parameters, you can create a function that returns an instance of the type and sets the specific values. (the general initialisation should still be done in new)


Ibmurai(Posted 2005) [#11]
Yes, but the Create function can't set the values in a Field, right? So if I want an object to be initialized with parameters upon creation... it just can't be done. I need to make a new object and the call some initialization method which sets the field values.... I'm sorry, I must be really confusing here... Let me make an example of what it is I want:

I want to create an object type like a house; And this house object takes a couple of parameters during creation, like roofheight and number of doors, and then it calculates, say, a threedimensional house model and stores the created vertices in some fields. And I just want to type something like "AHouse:THouse = new Thouse(roofheight, doorcount)" for all this to happen...


Mark Tiffany(Posted 2005) [#12]
Once you've created the instance in your Create function, you can set the fields and call the methods on it, i.e.:

Type THouse
  Field Floors:Int
  Function Create:THouse()
    Local tmpHouse:THouse=New THouse
    tmpHouse.Floors=2
    tmpHouse.MakeRoof
    Return tmpHouse
  End Function

  Method MakeRoof()
    'Some code
  End Method
End Type



JazzieB(Posted 2005) [#13]
Type rect
  Field x,y,w,h
  Field area

  Function Create:rect(sx,sy,sw,sh)
    Local r:rect=New rect
    r.x = sx
    r.y = sy
    r.w = sw
    r.h = sh
    r.area = sw*sh
    Return r
  EndFunction
EndType

Global block:rect=block.Create(10,10,250,120)

You can have a Create function that initialises the fields. You just need to make sure you're accessing them correctly.

Basically, Functions allow you to access all instances of a Type, for example when updating them or drawing them all. And Methods allow you to alter one particular instance.

EDIT: Ok, beaten to it!


BlitzSupport(Posted 2005) [#14]
You can declare default values for fields right there in the type:

Type Oink
    Field baa = 1
End Type



Hotcakes(Posted 2005) [#15]
A thing of beauty. I didn't know that...


Ibmurai(Posted 2005) [#16]
Thankyou everyone!

Most of my programming experience is with Delphi and most recently C/C++, so I got a bit confused there.

Thanks for clearing all this up for me - now I can really get started on my game.