incorrect 'range' values for optional parameters?

Started by Monsieur OUXX, Thu 12/07/2007 13:36:57

Previous topic - Next topic

Monsieur OUXX

The following code doesn't compile :

Code: ags

#define INT_MIN  -2147483647

struct MyStruct {
  	import static void MyFunc   (int value = INT_MIN);
};


the value of -2,147,483,647 comes from the manual
I always get the error : "default parameter out of range".

It appears the the minimum value for an optional parameter is -31999 ...  ???
 

Gilbert

Can you try if -32768 and 32767 works?

But anyway it's probably a minor bug, that the compiler considered the variable 16 bit instead of 32 bit.

Pumaman

It's because internally the compiler remembers default values as 16-bit ints. Is there any reason you can't just use -31999 as your default?

Monsieur OUXX

Quote32768

I tried that. The minimum value is -31999.

Quote from: Pumaman on Thu 12/07/2007 19:06:15
Is there any reason you can't just use -31999 as your default?

No, i could do it. I chose a value that would probably never be used (it's my 'NULL' value for ints).
 

Gilbert

@CJ, I think the compiler consider ALL default parameter values to be 16-bit int, right?
This may make it impossible to set default values to float parameters (see here), maybe you can consider some change in the next version?

Pumaman

The problem is really down to the fact that optional parameters were really only added to simplify a couple of the built-in function calls, but people latched onto the fact that they existed and started using them in custom scripts, and now the implementation of them doesn't really live up to what people want from the feature.

I'll have to think about it.

Monsieur OUXX

Quote from: Pumaman on Sat 28/07/2007 21:52:37
I'll have to think about it.

Don't worry about it, the complexity/usefullness ratio is not worth it, because of these problems :
1/ default values for chars would imply that they can be handled as numbers (or else, how would one define a default parameter for the char with ASCII code 0? He couldn't type in this character in the script editor!)
2/ default values for floats would be a bit pointless since these have to be handled in a very special way (comparison of two floats using '>' and '<' instead of simply '==', for example...)
 

monkey0506

Quote from: Monsieur OUXX on Wed 08/08/2007 23:12:141/ default values for chars would imply that they can be handled as numbers (or else, how would one define a default parameter for the char with ASCII code 0? He couldn't type in this character in the script editor!)

What are you talking about? The char type can be set by ASCII value directly by using it as if it were an int, or by using a character, i.e., 'a', 'b', 'c', etc. You can set a char to ASCII code 0 using:

Code: ags
char c = 0;


If you actually wanted to set its value to the character '0' (keycode 48) you could do either of the following:

Code: ags
char zero = 48;
// or:
char zero = '0';


Of course the char type is automatically bounded to the range 0-255. If you set a value outside this range, it will be forced into this range (I believe -1 = 255, -2 = 254, etc.). This actually led to some confusion when I was writing my versions of the EncryptedFile module because File.ReadRawChar actually returns an int. You can implicitly cast it into a char directly though like this:

Code: ags
char c = File.ReadRawChar();


Which I did in the module.

Monsieur OUXX

Quote from: monkey_05_06 on Thu 09/08/2007 04:04:00
Quote from: Monsieur OUXX on Wed 08/08/2007 23:12:141/ default values for chars would imply that they can be handled as numbers (or else, how would one define a default parameter for the char with ASCII code 0? He couldn't type in this character in the script editor!)

What are you talking about? The char type can be set by ASCII value directly by using it as if it were an int, or by using a character, i.e., 'a', 'b', 'c', etc. You can set a char to ASCII code 0 using:

Code: ags
char c = 0;


OK, i didn't know that. One less problem to implement default values for char, then :)


You may wonder why i thought it didn't work, since i used that in my own code. Well, erm... Let's say I wasn't sure it was working correctly.  ;D
 

SMF spam blocked by CleanTalk