Skip to main content
Known Participant
February 27, 2009
Question

Cfinterface returntype limitation

  • February 27, 2009
  • 13 replies
  • 1450 views
It seems that the returntype validation in cfinterface and implementing classes is a bit too limiting....I can't see how a CFC function could ever return an instance of itself.

For example, let's say I have an IOrder interface with an init() function where the returntype="IOrder". Then I create an Order.cfc that implements IOrder and add my init() function with returntype="Order". CF blows up at object creation and says the return types do not match. I think this is mistaken, since the Order returntype is in fact an instance of IOrder.

Is there something I'm missing? I know I can use "any" in both functions as a workaround, but that undermines the whole point of using strict interfaces in the first place.
This topic has been closed for replies.

13 replies

Inspiring
February 28, 2009
> why would you want to define a constructor
> for an interface?

Because one might want to ensure the class provides a constructor taking a
specific combination of arguments.

In the context of CF, there is no concept of constructors, so init() is
just a method. CF has no way of knowing there's a notional agreement in
the community that init() methods are generally used in lieu of a
constructor.

--
Adam
Inspiring
February 28, 2009
> It seems that the returntype validation in cfinterface and implementing classes
> is a bit too limiting....I can't see how a CFC function could ever return an
> instance of itself.

This works OK for me.

<!--- IFoo.cfc --->
<cfinterface>
<cffunction name="getSelf" returntype="IFoo">
</cffunction>
</cfinterface>

<!--- Foo.cfc --->
<cfcomponent implements="shared.cf.cfml.I.interface.IFoo">
<cffunction name="getSelf" returntype="IFoo">
<cfreturn this>
</cffunction>
</cfcomponent>

<!--- foo.cfm --->
<cfset o = createObject("component", "Foo").getSelf()>
<cfdump var="#o#">


> Is there something I'm missing?

I think you're missing the part that if you're having some problems with
your code, it's always a good idea to post some code that demonstrates the
problem. Other than that: it's hard to tell.

--
Adam
Inspiring
February 27, 2009
Ignoring the question for a moment, why would you want to define a constructor for an interface? Using the java model, interfaces generally define a contract for methods of a class (ie functions of a component), and not constructors. Interfaces are not meant to be instantiated. Though technically the init() function straddles both worlds, it is really more of a constructor than a method/function.

BKBK
Community Expert
Community Expert
March 6, 2009
Rich.thibault and -==cfSearching==-, you are right, and I am wrong. The example I gave contradicts everything I had just said. I have corrected it accordingly. Thanks.