Skip to main content
WolfShade
Legend
June 14, 2011
Question

Dynamic returntype in CFC function?

  • June 14, 2011
  • 5 replies
  • 2239 views

Is there a way to dynamically set a function returntype in a CFC?

For example; if I have a function that contains a 700 line query, and need it to work with two different objects, each of which requires a different format..

.. instead of having two 700+ line functions, one to return a query and the other to return a struct, is there a way to tell the function which format to return the data in?  Can it be passed using CFARGUMENT?

Thanks,

^_^

    This topic has been closed for replies.

    5 replies

    BKBK
    Community Expert
    Community Expert
    June 16, 2011

    WolfShade wrote:

    Is there a way to dynamically set a function returntype in a CFC?

    For example; if I have a function that contains a 700 line query, and need it to work with two different objects, each of which requires a different format..

    .. instead of having two 700+ line functions, one to return a query and the other to return a struct, is there a way to tell the function which format to return the data in?  Can it be passed using CFARGUMENT?

    Yes, why not. You could do it as follows (with a choice to pass the argument returnType="query" or returnType="struct" to the function):

    <cfcomponent>

    <cffunction returntype="any">

    <cfargument name="returnType">

    ...

    ...

    <cfif arguments.returnType is "query">

    <cfreturn someQuery>

    <cfelse>

    <cfreturn someStruct>

    </cfif>

    </cffunction>

    </cfcomponent>

    Inspiring
    June 15, 2011

    Is there a way to dynamically set a function returntype in a CFC?

    It looks like your requirement here has changed anyhow, but to answer the question you actually asked: no, this is not possible.  The returntype is needed at compile time, whereas any dynamic determination is done at runtime, after the code is already compiled.

    For example; if I have a function that contains a 700 line query,

    Really? Ye gods.  How?

    and need it to work with two different objects, each of which requires a different format..

    As Dan said: have two functions.  One which returns the query, one which calls that function, messes with it, and returns a struct.

    Someone suggested using returntype="any", and whilst that's possible, having one function which could possibly return two different types of data is... um... "less than ideal".  As a rule, a function should do one thing, so having code that either does x or does y is doing two things.

    --

    Adam

    Inspiring
    June 15, 2011

    Regarding "how to write a 700 line query".  I often use short lines with commas at the front because it makes it easier to comment out selected lines.  It does add to the query length though.

    insert into sometable (

    field1

    , field2

    ...

    , fieldn)

    values (

    value1

    , value2

    ...

    , valuen)

    I recently wrote an update query that is 180 lines long.  It would have been longer but it has two loops inside it.

    Inspiring
    June 15, 2011

    Regarding "how to write a 700 line query".  I often use short lines with commas at the front because it makes it easier to comment out selected lines.  It does add to the query length though.

    Sure.  Me too.  But 700 lines?

    --

    Adam

    Inspiring
    June 14, 2011

    I would write 2 functions.  The first would contain all the sql and return a query.  The second would call the first and process accordingly.

    WolfShade
    WolfShadeAuthor
    Legend
    June 14, 2011

    Thanks to everyone who offered advice/suggestions.

    Sadly, the developer I was helping just learned that it was all for nought.  Turns out the CFGRID that he was using for one of the two situations - isn't allowed to be used by the IT department, here (they are ultra-paranoid of security risks, and a Java object is a security risk.)

    So, the developer is now going to use JavaScript to try and accomplish the same goal.

    ^_^

    Participating Frequently
    June 14, 2011

    Just set returntype="any". Problem solved and works a charm.

    Inspiring
    June 14, 2011

    Why not use the CFC return type of "any" ?

    WolfShade
    WolfShadeAuthor
    Legend
    June 14, 2011

    If returntype is "any", won't it always return the native form of the data, regardless?

    If an object (like, say, CFGRID) needs the data returned as a struct instead of a query, won't it still get a query?

    ^_^

    Participating Frequently
    June 14, 2011

    The return will be whatever is in the <cfreturn parameter, pure and simple.

    If the method returns a struct, it will be a struct, if it is told to

    return a query, it will return a query. The only thing the returntype does

    for you is validate the outgoing datatype at runtime. It can't change the

    data, it will just tell you if it's wrong. If the returntype truly may be

    either a struct or a query, then use 'any' and the CFC won't complain at

    runtime. No other real downside.

    Now, the calling code has to know what to expect, and, yes, the calling

    code could certainly tell the function what datatype it wanted back, by

    setting a cfargument in the method call, but the returntype has to be 'any'

    at that point as well.