Jump to page: 1 2
Thread overview
Pop quiz, what does this do?
Mar 03, 2021
Stefan Koch
Mar 03, 2021
Stefan Koch
Mar 03, 2021
Simen Kjærås
Mar 03, 2021
Stefan Koch
Mar 03, 2021
Simen Kjærås
Mar 03, 2021
Max Haughton
Mar 03, 2021
Dukc
Mar 03, 2021
H. S. Teoh
March 03, 2021
The FieldNameTuple from std.traits returns a tuple of names of all the fields that are present on a type instance.

What about the result on things that can't have fields?

import std.traits;

interface I
{
   int foo();
}

pragma(msg, FieldNameTuple!I);

Without looking up the docs, or trying it, what do you think it does?

-Steve
March 03, 2021
On Wednesday, 3 March 2021 at 16:06:48 UTC, Steven Schveighoffer wrote:
> The FieldNameTuple from std.traits returns a tuple of names of all the fields that are present on a type instance.
>
> What about the result on things that can't have fields?
>
> import std.traits;
>
> interface I
> {
>    int foo();
> }
>
> pragma(msg, FieldNameTuple!I);
>
> Without looking up the docs, or trying it, what do you think it does?
>
> -Steve

My feeling is that it returns tuple("foo").
But based on the name that probably shouldn't happen.
March 03, 2021
On Wednesday, 3 March 2021 at 16:31:53 UTC, Stefan Koch wrote:
> On Wednesday, 3 March 2021 at 16:06:48 UTC, Steven Schveighoffer wrote:
>> The FieldNameTuple from std.traits returns a tuple of names of all the fields that are present on a type instance.
>>
>> What about the result on things that can't have fields?
>>
>> import std.traits;
>>
>> interface I
>> {
>>    int foo();
>> }
>>
>> pragma(msg, FieldNameTuple!I);
>>
>> Without looking up the docs, or trying it, what do you think it does?
>>
>> -Steve
>
> My feeling is that it returns tuple("foo").
> But based on the name that probably shouldn't happen.

Huh ... it works as it should in this case.
Pleasant surprise :)
March 03, 2021
On Wednesday, 3 March 2021 at 16:06:48 UTC, Steven Schveighoffer wrote:
> The FieldNameTuple from std.traits returns a tuple of names of all the fields that are present on a type instance.
>
> What about the result on things that can't have fields?
>
> import std.traits;
>
> interface I
> {
>    int foo();
> }
>
> pragma(msg, FieldNameTuple!I);
>
> Without looking up the docs, or trying it, what do you think it does?

I remember seeing that a couple years back (https://forum.dlang.org/post/hwrsxypruzeffreqxasa@forum.dlang.org). It seems intentional, but also very, very weird.

Accordion to the PR (https://github.com/dlang/phobos/pull/2561), it was for consistency. Consistency with what, however, is unclear.

--
  Simen
March 03, 2021
On Wednesday, 3 March 2021 at 16:06:48 UTC, Steven Schveighoffer wrote:
> The FieldNameTuple from std.traits returns a tuple of names of all the fields that are present on a type instance.
>
> What about the result on things that can't have fields?
>
> import std.traits;
>
> interface I
> {
>    int foo();
> }
>
> pragma(msg, FieldNameTuple!I);
>
> Without looking up the docs, or trying it, what do you think it does?
>
> -Steve

"" - difference between a field and a member I'm guessing
March 03, 2021
On 3/3/21 11:33 AM, Stefan Koch wrote:
> On Wednesday, 3 March 2021 at 16:31:53 UTC, Stefan Koch wrote:
>> On Wednesday, 3 March 2021 at 16:06:48 UTC, Steven Schveighoffer wrote:
>>> The FieldNameTuple from std.traits returns a tuple of names of all the fields that are present on a type instance.
>>>
>>> What about the result on things that can't have fields?
>>>
>>> import std.traits;
>>>
>>> interface I
>>> {
>>>    int foo();
>>> }
>>>
>>> pragma(msg, FieldNameTuple!I);
>>>
>>> Without looking up the docs, or trying it, what do you think it does?
>>>
>>
>> My feeling is that it returns tuple("foo").
>> But based on the name that probably shouldn't happen.
> 
> Huh ... it works as it should in this case.
> Pleasant surprise :)

It works as designed. It's the design I question.

For instance, code like:

foreach(m; FieldNameTuple!T)
   // do something with __traits(getMember, T, m)

will fail. Not because FieldNameTuple complains that it can't have field names, but because of what it's hard-coded to do.

-Steve
March 03, 2021
On 3/3/21 11:41 AM, Simen Kjærås wrote:
> On Wednesday, 3 March 2021 at 16:06:48 UTC, Steven Schveighoffer wrote:
>> The FieldNameTuple from std.traits returns a tuple of names of all the fields that are present on a type instance.
>>
>> What about the result on things that can't have fields?
>>
>> import std.traits;
>>
>> interface I
>> {
>>    int foo();
>> }
>>
>> pragma(msg, FieldNameTuple!I);
>>
>> Without looking up the docs, or trying it, what do you think it does?
> 
> I remember seeing that a couple years back (https://forum.dlang.org/post/hwrsxypruzeffreqxasa@forum.dlang.org). It seems intentional, but also very, very weird.

Oh snap! almost 2 years ago, I had the same exact WTF moment.

Except this time, it is affecting code that I have to fix.

> Accordion to the PR (https://github.com/dlang/phobos/pull/2561), it was for consistency. Consistency with what, however, is unclear.

Thanks for the link. I think it's consistently uninutitive...

-Steve
March 03, 2021
On Wednesday, 3 March 2021 at 16:06:48 UTC, Steven Schveighoffer wrote:

> import std.traits;
>
> interface I
> {
>    int foo();
> }
>
> pragma(msg, FieldNameTuple!I);
>
> Without looking up the docs, or trying it, what do you think it does?

I'd think it either returns nothing, or the typeid and monitor fields (or whatever they are)  that all classes are supposed to have.
March 03, 2021
On 3/3/21 11:42 AM, Max Haughton wrote:
> On Wednesday, 3 March 2021 at 16:06:48 UTC, Steven Schveighoffer wrote:
>> The FieldNameTuple from std.traits returns a tuple of names of all the fields that are present on a type instance.
>>
>> What about the result on things that can't have fields?
>>
>> import std.traits;
>>
>> interface I
>> {
>>    int foo();
>> }
>>
>> pragma(msg, FieldNameTuple!I);
>>
>> Without looking up the docs, or trying it, what do you think it does?
>>
>> -Steve
> 
> "" - difference between a field and a member I'm guessing


No, it's not. A member that's not a field doesn't normally produce anything in the tuple.

Try this one:

interface I
{
   int foo();
   int bar();
}

-Steve
March 03, 2021
On Wednesday, 3 March 2021 at 16:33:35 UTC, Stefan Koch wrote:
> On Wednesday, 3 March 2021 at 16:31:53 UTC, Stefan Koch wrote:
>> On Wednesday, 3 March 2021 at 16:06:48 UTC, Steven Schveighoffer wrote:
>>> The FieldNameTuple from std.traits returns a tuple of names of all the fields that are present on a type instance.
>>>
>>> What about the result on things that can't have fields?
>>>
>>> import std.traits;
>>>
>>> interface I
>>> {
>>>    int foo();
>>> }
>>>
>>> pragma(msg, FieldNameTuple!I);
>>>
>>> Without looking up the docs, or trying it, what do you think it does?
>>>
>>> -Steve
>>
>> My feeling is that it returns tuple("foo").
>> But based on the name that probably shouldn't happen.
>
> Huh ... it works as it should in this case.
> Pleasant surprise :)

No it doesn't. Look again.

--
  Simen
« First   ‹ Prev
1 2