Can I use a CONST in a Function within a Type?

BlitzMax Forums/BlitzMax Programming/Can I use a CONST in a Function within a Type?

ImaginaryHuman(Posted 2005) [#1]
Does anyone know if it's possible to define a constant using a variable, within a new type?

For example something like:

Global myvar:int=12345

Type mytype
   Field x:int
   Field y:int
   Field Const z:myvar

   'or

   Function myfunct()
      Const myfixedvar=myvar
      'do stuff
   End Function
EndType

a:mytype=New mytype

Something like that... so that as you create a new instance of the type, including a new function, the constant is declared but it's actually set to the content of another variable?

I'm trying to set up a new function at runtime which has some customization each time I create a new instance of it. If I could set up a const for the function or the type and set it to the content of a Global variable, it would really help.


fredborg(Posted 2005) [#2]
Global myvar:Int = 12345
Type mytype
Field z:Int
Method New()
Self.z = myvar
EndMethod
EndType
a:mytype = New mytype
Print a.z
Is about as close as you'll get.

This is another (albeit silly) option:
Const myvar:Int = 12345
Type mytype
	Const z:Int = myvar
EndType
a:mytype = New mytype
Print a.z



ImaginaryHuman(Posted 2005) [#3]
Hmm. Thanks. It's not quite what I was trying to do. I am trying to set up the function so that I don't have to explicitly read any variables in order to access the values. If I had a method to set the one of the fields in the type, that'd be no different than just reading the field when the function is called. I'm trying to get around that. I don't like using variables where I don't have to. Variables are evidence that things are uncertain and if you can be more certain about what is happening things are more efficient.

It'd be useful to me to be able to define the function and have embedded in that function a constant, so that the constant might end up as immediate data for an instruction rather than have to be read in additionally from memory.


fredborg(Posted 2005) [#4]
Method New()

Is automatically called when you create a new type instance.


ImaginaryHuman(Posted 2005) [#5]
Oh really? That's good to know.

But it would still set a field variable to a given value. I am trying to do away with the field variable.

It's not as important to pass the variable into the function as it is to bypass the need for a variable altogether.


fredborg(Posted 2005) [#6]
Well, a constant in BlitzMax is constant, and has to be fully defined at compile time.

If I understood your original question, you want to have the 'z' constant hold a different value for each type instance you make. Which to me sounds like a variable constant. That is simply not possible, and you will have to make 'z' a regular variable.

If you simply want the same constant value in all type instances, you could use the second example I posted, but would be better off with a regular constant.


Robert(Posted 2005) [#7]
Why not use a Global variable inside the type?


ImaginaryHuman(Posted 2005) [#8]
I understand now that I wont be able to use a Const that differs with each new function-within-a-type, because they are set at compile-time. Probably I could define a Const to a given value in the type definition but it would have to hold the same value in all the created functions. So the original idea of this thread is void.

But anyway, this is what I'm trying to do...

I have an array of function pointers - default function pointers - which is initialized at startup to hold one function pointer for each of several functions that already exist in my program. These are in a fixed order.

Then, I have another array of function pointers, this time they can be any of the function pointers taken from the default array and in any order and repeated any number of times.

In order to go through the customized array of function pointers and execute those functions, I want to use a single instruction which is something like:

myfunctionarray[index]()

This is fine for executing the functions that existed already at compile time. However, while the program runs, there will come a time when I need to branch off from the current array of function pointers and temporarily go through a second one, kind of like a `gosub`. Since there can be any number of such arrays I give each a reference number.

Now, I could insert an `immediate value` into a function pointer array which is not a function pointer (using a static bank) which could act as immediate data to be read in as the number of the array to be jumped to. But if I do that, I will need to complicate the piece of code that goes through the function pointers executing them.

So I figured that if an actual function existed that represents each new array of function pointers, I could get THAT function pointer and just refer to at any point in order to `execute` the new array.

So I was hoping that, with each new array of function pointers, I would add an instance of a type that contains a function. I would then get the function pointer for that new function and add it to the array of default function pointers. (there's a reason why I need that default array) Then I can use that function pointer in any other array to call on this new array. I know it all sounds complicated.

So what's the problem? Well, the function within the instance of the type needs to know what number I have assigned to that array. The function would then probably add that number to a list of numbers, referring to which arrays are currently active. The active arrays are the ones that are `executed`. I wanted to find a way that I could somehow customize each instance of the function in such a way that I wouldn't have to access a variable each time, otherwise with every call to a new array of function pointers there is going to be an overhead.

I have another alternative to how to do this but it would not be any faster or slower than trying to do it by adding a variable to the new type that's created, containing the array number. I am trying to find out if there are any possibilities for streamlining it further. I know this sounds like I'm niggling about a dumb little variable that means nothing but I like to explore these things. ;-)

So there you go, it sounds pretty complicated, but I just wanted to know if, when I created a new type with a function in it, I could somehow get that function to automatically activate the right program rather than having to read a variable to find out the number.

*throws hands up in the air*