0

return type confusion

ColdFusion

I thought that the expected behavior for a <cffunction> with a returntype other than void would be to throw an error in the event that nothing is returned. 

<cffunction name="test" output="false" access="public" returntype="string">
	<cfset var foo = "bar" />
	<!--- Notice this code doesn't return anything, although the return type is defined as a string --->
</cffunction>

<!--- Invoke the function --->
<cfset blah = test() />

<!--- Have a look at what's in here --->
<cfdump var="#blah#" />	

The above code doesn't really do much, but it should throw an error, right?  An error that "the value returned from the test function is not of type string", right?  Au contraire ..

Needless to say I found this quite surprising.  Shouldn't the method have exploded all over the place before I got the chance to evaluate the non-existing blah?  I got even more confused when I changed the return type to numeric and saw something different:

Any thoughts?  Shouldn't the behavior be the same in both cases?  Isn't the second behavior the expected one?  Would appreciate some help here.

Mark said:
 
If you do not cfreturn anything from the function, the return type is probably set to void, null or nothing, which explains why variable blah is undefined or not of type numeric.
 
posted 501 days ago
Add Comment Reply to: this comment OR this thread
 
erichk said:
 
@Mark - Yes, that is precisely what should happen. However, the error should occur regardless of the return type declared on the function since nothing is being returned, shouldn't it? Why doesn't this happen for a return type of string?
 
posted 500 days ago
View Replies (1) || Add Comment Reply to: this comment OR this thread
 
.: HIDE REPLIES :.
Mark said:
 
Easy. My guess is CF gets null (or void) as a return value, and compares it to the declared return type. A String-typed variable can be of value null, but a numeric-typed value cannot. When you assign null to variable blah, it's like to assign a reference a value of null - the variable ceases to exist, hence the error message. IMHO.
 
posted 500 days ago
Add Comment Reply to: this comment OR this thread
 

Search