Actually, I think you would find it problematic doing this.
Let's say we have the following:
struct A {
1 : i32 v;
}
struct B {
1: i32 w;
}
list<any> foo(...)
Assume the response to a call to foo() is:
T_LIST 2 T_STRUCT 1: I32 ... T_STOP 1: I32 ... T_STOP
There are many valid interpretations of the type: it could be a list of
A,A or list of A,B, or list of B,B.
In some languages, this would not be an issue - you can pick any of the
combos, and it would not cause any problems, since you can't in general
distinguish between an object of type A and one of type B (particularily
if the names of the fields are irrelevant). But in a strongly typed
language such as C++, it may.
More problematic, even for a dynamically typed language, alter the type
of A to be:
struct A {
1: i32 v;
2: optional i32 opt = 100
}
Now, what is the type of the value returned by foo?
If later, we have the code:
x = foo(....);
print x[0].opt
then clearly we expect element 0 to have been an A.
Even nastier, change B:
struct B {
1: i32 w;
2: optional i32 opt = 200
}
Now, given the response received above:
x= foo(...)
print x[0].opt, x[1].opt
The values that are printed will depend on the demarshaller's choice of
type. This may be different than the types that were actually transmitted.
Bryan Duxbury wrote:
Generally, we've said that it could be possible, but we haven't made it so
yet.
My personal preference would be not to support this. I understand the need
for heterogeneous collections, but I think that "any" is a pretty broad
category to allow in a collection and not have to compromise something.
There is already a totally viable workaround, though - make a Union of the
types you want in your collection, and then make the field list<YourUnion>.
You get basically all the capabilities with very few drawbacks, plus the
ability to include multiple logical "types" in the collection, not just
physical types. Of course, if you literally need "any" possible object to go
into the collection, then this won't do it for you.
-Bryan
On Sat, May 1, 2010 at 10:40 AM, Alex Boisvert <[email protected]> wrote:
Hi,
I'd like to use an heterogeneous collection, e.g. list<any>, in a service.
I see there's been some work/discussion on this earlier,
https://issues.apache.org/jira/browse/THRIFT-122
https://issues.apache.org/jira/browse/THRIFT-110
Now THRIFT-110 is committed and claims to incorporate THRIFT-122.
I've looked around but couldn't find the answer... how do I declare an
heterogeneous collection in a .thrift definition?
alex