I'm a fan of using required arguments with types where appropriate. I want to write a function which requires an integer as an argument and errors if it is passed anything else. Generally I have been using required numeric num
however numeric
will accept floats and doubles etc. I understand from the adobe cfparam docs (although I much prefer the cfdocs version) that integer
is a valid parameter type.
What I don't understand is how to create an integer in the first place. I gather CF does some things under the hood storing numbers as strings but as you can see from the cfscript below, I've tried a few different things, and got their types from getMetaData()
and none of them are actually integers.
To add to my confusion, many of these types return true
from isValid("integer", num)
whilst still failing required integer num
.
Most grateful if you can point me in the right direction.
private void function printMeta( required num ){
writeOutput( '</br>' );
writeOutput( ARGUMENTS.num );
writeOutput( '</br>' );
writeOutput( getMetaData( ARGUMENTS.num ).getName() );
writeOutput( '</br>' );
writeOutput( 'isValid integer? ' & isValid( 'integer', ARGUMENTS.num ) );
writeOutput( '</br>' );
}
private numeric function addOne( required numeric num ){
return ARGUMENTS.num + 1;
}
private function needsInt( required integer num ){
return "what a lovely integer you've got there, it's " & ARGUMENTS.num;
}
test1 = 2;
printMeta( test1 );
test2 = addOne( test1 );
printMeta( test2 );
test3 = Int( 7 );
printMeta( test3 );
test4 = numberFormat( 8 );
printMeta( test4 );
try {
test5 = needsInt( Int( 7 ) );
}
catch(any err) {
WriteOutput('<br />');
WriteDump( err.message );
WriteOutput('<br />');
}
try {
test6 = needsInt( test2 );
}
catch(any err) {
WriteOutput('<br />');
WriteDump( err.message );
WriteOutput('<br />');
}
test7 = 3.5;
printMeta( test7 );
test8 = addOne( test7 );
printMeta( test8 );
Outputs:
2 java.lang.String isValid integer? YES
3 java.lang.Double isValid integer? YES
7 java.lang.Long isValid integer? YES
8 java.lang.String isValid integer? YES
The NUM argument passed to the needsInt function is not of type integer.
The NUM argument passed to the needsInt function is not of type integer.
3.5 java.lang.String isValid integer? NO
4.5 java.lang.Double isValid integer? NO
The NUM argument passed to the needsInt function is not of type integer.
Edit I tried specifically creating a java integer and still didn't manage to get it to work;
test11 = createObject("java","java.lang.Integer").parseInt("5");
printMeta( test11 );
try {
test12 = needsInt( test11 );
}
catch( any err ) {
WriteOutput('<br />');
WriteDump( err.message );
WriteOutput('<br />');
}
Ouputs:
5 java.lang.Integer isValid integer? YES
The NUM argument passed to the needsInt function is not of type integer.
I don't actually know what your precise question is, but I think your problem is that there is no native argument type integer
in CFML. It's just numeric
. This will take any value than can be coerced into a numeric value: strings containing only digits, booleans, dates, stuff like that).
If you have this:
function f(required integer x)
Then CFML will interpret that as being of type integer.cfc. Not "an integer" in the comp-sci sense, nor a Java int primitive, or a java.lang.Integer etc.
If you need your argument to be an actual integer, then use numeric
, then validate it as an integer within the function body, eg:
function f(required numeric x) {
param integer x;
// ...
}
Ugly, but does the trick.
Here's an example on trycf.com
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With