Determining whether a class has a method
Christopher Wright
dhasenan at gmail.com
Thu Nov 29 20:15:21 PST 2007
Bill Baxter wrote:
> Christopher Wright wrote:
>> Jarrett Billingsley wrote:
>>> "Christopher Wright" <dhasenan at gmail.com> wrote in message
>>> news:finqpt$lnj$2 at digitalmars.com...
>>>> Jarrett Billingsley wrote:
>>>>> "Christopher Wright" <dhasenan at gmail.com> wrote in message
>>>>> news:fink7k$6ph$1 at digitalmars.com...
>>>>>
>>>>>> But tupleof fails if you give it a class that has fields (methods
>>>>>> are fine, but data fields aren't). I'm pretty sure this is a bug;
>>>>>> can anyone else comment on it?
>>>>> tupleof is supposed to give you a tuple of fields. It will never
>>>>> give methods.
>>>> And it's supposed to work for classes?
>>>
>>> Yes. It's all in the spec:
>>> Class Properties
>>> The .tupleof property returns an ExpressionTuple of all the fields in
>>> the class, excluding the hidden fields and the fields in the base class.
>>
>> Ah. Then it's not supposed to generate an error:
>> Error: type Foo is not an expression
>>
>> For reference, here's the code, and I think it's not my error:
>>
>> class Foo {
>> // Comment the following line to eliminate the error:
>> // Error: type Foo is not an expression
>> int i;
>> }
>>
>> foreach (blah; Foo.tupleof) {
>> writefln("%s", blah.stringof);
>> }
>> // or:
>> auto a = Foo.tupleof[0];
>
> Foo is a type. So Foo.tupleof would be a type tuple (if anything -- I'm
> not sure that works, though it does for structs).
>
> You probably need
>
> auto a = (new Foo).tupleof[0];
>
> --bb
If you want to use it as a type, though, you have to do
typeof(Foo.tupleof[0]).
But you still can't do (Foo.tupleof[0]).stringof ("Error: Foo.tupleof is
used as a type").
More information about the Digitalmars-d-learn
mailing list