Just to be sure I'm not missing anything-- is ther...
# cfml-general
b
Just to be sure I'm not missing anything-- is there a CFML BIF that will tell me if a variable contains a CFC instance specifically? The
isObject()
BIF in CF returns
true
for basically any unrecognized Java class which makes it sort of useless in many instances where a framework needs to deal smartly with any sort of input.
On a related note, you can pass Java classes into
getMetaData()
without error and you'll basically get back a
java.lang.Class
that represents what the class is (differs for static refences vs instances)
At least, you usually can eye roll https://luceeserver.atlassian.net/browse/LDEV-4259
w
there's isValid("component",value) fwiw, not sure if that's what you're after
i have a udf that sequentially checks datatypes that may help to resolve any ambiguity if you want it
b
Ahh yes,
isValid()
may be what I'm after. It's a little odd that CFML has
isEverythingElseUnderTheSun()
but not
isComponen()
And I've always thought
isObject()
was a little ambiguous since in Java EVERYTHING'S an object, even strings!
So
isObject()
will return true for
java.lang.System
, but not
java.lang.String
!
Having a CF BIF that tell me when a variable contains a Java object is a little... redundant. And then just silly when even that doesn't work all the time, lol
w
wish they had a bif getDatatype() function, but here's one i've used for years and have found the sequence to be important and sound:
Copy code
/**
     * Returns the datatype of the object's value
     * 
     * @value any                       the value whose datatype to determine
     * 
     * @return string                   the datatype of the provided argument
     */
    public string function getDatatype( required any value ) output=false {

        if( isValid( "array", arguments.value ) ) { return 'Array'; }
        else if( isValid( "binary", arguments.value ) ) { return 'Binary'; }
        else if( isValid( "numeric", arguments.value ) ) { return 'Numeric'; }
        else if( isValid( "date", arguments.value ) ) { return 'Date'; }
        else if( isValid( "query", arguments.value ) ) { return 'Query'; }
        else if( isValid( "struct", arguments.value ) ) { return 'Struct'; }
        else if( isWDDX( arguments.value ) ) { return 'WDDX'; }
        else if( isJSON( arguments.value ) ) { return 'JSON'; }
        else if( isValid( "boolean", arguments.value ) ) { return 'Boolean'; }
        else if( isValid( "component", arguments.value ) ) { return 'Component'; }
        else if( isCustomFunction( arguments.value ) ) { return 'Function'; }
        else if( isSimpleValue( arguments.value ) ) { return 'String'; }
        else { return 'Unknown'; }
        
    }
👍 3
b
isClosure()
should probably apply to
Function
above
w
missing a couple things like a check for xml, but i guess i never needed it
b
I've always thought it was wrong and silly that CF had both
isCustomFunction()
and
isClosure()
w
can it reliably tell the difference?
b
Should have just been
isUDF()
all along IMO
I assume it can, but I have yet to ever care in my code how a given UDF was declared.
If I have a higher order function (one that accepts other functions) then it makes no difference at all how the incoming UDF was declared!
✔️ 1
b
Geez, just read this in there
Note: The isInstanceOf function returns false if the CFC specified by the object parameter does not define any functions.
Like... why?
That option seems to work pretty well
Copy code
CFSCRIPT-REPL: IsInstanceOf(23324,'Component')
false
CFSCRIPT-REPL: IsInstanceOf(new query(),'Component')
true
CFSCRIPT-REPL: IsInstanceOf(new http(),'Component')
true
CFSCRIPT-REPL: IsInstanceOf('sdf','Component')
false
CFSCRIPT-REPL: IsInstanceOf([],'Component')
false
CFSCRIPT-REPL: IsInstanceOf({},'Component')
false
CFSCRIPT-REPL: IsInstanceOf(xmlNew(),'Component')
false
CFSCRIPT-REPL: IsInstanceOf(createObject('java','java.lang.System'),'Component')
false
It seems it will accept pretty much any input without blowing up
g
The documentation for isInstanceOf() is pretty sparse. Your comment here has better info than existing documentation on CF DOCS, Adobe & Lucee sites.
💯 1