Create an integer to use with an integer parameter in ColdFusion

I am a fan of using the necessary arguments with types where necessary. I want to write a function that requires an integer as an argument and errors if it is passed something else. As a rule, I used required numeric num, however, it numericwill accept floats and doubles, etc. I understand from adobe cfparam docs (although I prefer the cfdocs version ), which integeris a valid parameter type.

I do not understand how to create an integer in the first place. I think CF does some things under the hood, storing numbers as strings, but as you can see from cfscript below, I tried several different things and got their types from getMetaData(), and none of them are actually integer by number.

To add to my confusion, many of these types are returned truefrom isValid("integer", num), while still not working required integer num.

Very 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 " & 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 needsInt is not of type integer.

The NUM argument passed to needsInt 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 needsInt is not of type integer.

java- ;

test11 = createObject("java","java.lang.Integer").parseInt("5");
printMeta( test11 );

    try {
    test12 = needsInt( test11 );
}
catch( any err ) {
    WriteOutput('<br />');
    WriteDump( err.message );
    WriteOutput('<br />');
}     

:

5 java.lang.Integer isValid integer?

NUM, needsInt, integer.

+6
1

, , , , CFML integer. numeric. , : , , , ..).

:

function f(required integer x)

CFML integer.cfc. " " comp-sci, Java int, java.lang.Integer ..

, numeric, , :

function f(required numeric x) {
    param integer x;
    // ...
}

, .

trycf.com

+9

All Articles