Jump to page: 1 2
Thread overview
How get struct value by member name string ?
May 29, 2023
John Xu
May 29, 2023
Adam D Ruppe
May 30, 2023
John Xu
May 30, 2023
H. S. Teoh
May 30, 2023
John Xu
May 30, 2023
drug007
May 30, 2023
bauss
May 30, 2023
bauss
May 31, 2023
John Xu
Jun 02, 2023
John Xu
Jun 02, 2023
John Xu
Jun 02, 2023
bauss
May 29, 2023

I saw ddbc (https://github.com/buggins/ddbc/blob/master/source/ddbc/pods.d) uses

static if (__traits(compiles, (typeof(__traits(getMember, T, m))))) {
    __traits(getMember, T, m)
}

But for my experience, above code sometimes/somewhere works, sometimes/somewhere just doesn't:

Error: variable `column` cannot be read at compile time

Is there any friend can explain some more details?

May 29, 2023
On Monday, 29 May 2023 at 09:35:11 UTC, John Xu wrote:
>     Error: variable `column` cannot be read at compile time

you should generally getMember on a variable

T t;
__traits(getMember, t, "name")

like that, that's as if you wrote t.name
May 30, 2023
On Monday, 29 May 2023 at 11:21:11 UTC, Adam D Ruppe wrote:
> On Monday, 29 May 2023 at 09:35:11 UTC, John Xu wrote:
>>     Error: variable `column` cannot be read at compile time
>
> you should generally getMember on a variable
>
> T t;
> __traits(getMember, t, "name")
>
> like that, that's as if you wrote t.name

It seems I can't use variable as member name:

    struct T {int a; string name;}
    T t;
    string s = "name";
    writeln(__traits(getMember, t, s));

Above code fails to compile. Any help?
May 29, 2023
On Tue, May 30, 2023 at 01:24:46AM +0000, John Xu via Digitalmars-d-learn wrote:
> On Monday, 29 May 2023 at 11:21:11 UTC, Adam D Ruppe wrote:
> > On Monday, 29 May 2023 at 09:35:11 UTC, John Xu wrote:
> > >     Error: variable `column` cannot be read at compile time
> > 
> > you should generally getMember on a variable
> > 
> > T t;
> > __traits(getMember, t, "name")
> > 
> > like that, that's as if you wrote t.name
> 
> It seems I can't use variable as member name:
> 
>     struct T {int a; string name;}
>     T t;
>     string s = "name";
>     writeln(__traits(getMember, t, s));
> 
> Above code fails to compile. Any help?

Short answer:

`s` must be known at compile-time.  Or more precisely, known at the time of template expansion. In this case, use `enum`:

	enum s = "name";


Long answer:
	https://wiki.dlang.org/Compile-time_vs._compile-time


T

-- 
Which is worse: ignorance or apathy? Who knows? Who cares? -- Erich Schubert
May 30, 2023
On Tuesday, 30 May 2023 at 01:33:54 UTC, H. S. Teoh wrote:
> On Tue, May 30, 2023 at 01:24:46AM +0000, John Xu via Digitalmars-d-learn wrote:
>> On Monday, 29 May 2023 at 11:21:11 UTC, Adam D Ruppe wrote:
>> > On Monday, 29 May 2023 at 09:35:11 UTC, John Xu wrote:
>> > >     Error: variable `column` cannot be read at compile time
>> > 
>> > you should generally getMember on a variable
>> > 
>> > T t;
>> > __traits(getMember, t, "name")
>> > 
>> > like that, that's as if you wrote t.name
>> 
>> It seems I can't use variable as member name:
>> 
>>     struct T {int a; string name;}
>>     T t;
>>     string s = "name";
>>     writeln(__traits(getMember, t, s));
>> 
>> Above code fails to compile. Any help?
>
> Short answer:
>
> `s` must be known at compile-time.  Or more precisely, known at the time of template expansion. In this case, use `enum`:
>
> 	enum s = "name";
>
>
> Long answer:
> 	https://wiki.dlang.org/Compile-time_vs._compile-time
>
>
> T


How to put above enum as a function parameter? Following code wouldn't work:

    string getTMember(T t, enum string memberName) {
        return __traits(getMember, t, memberName);
    }

My database table is very wide, with many columns. Above ddbc allows a struct
to map db returned data. Then if I want a member's value to show in vibe.d template,
how do I use a function to get it?
May 30, 2023
30.05.2023 11:46, John Xu пишет:
> 
> 
> How to put above enum as a function parameter? Following code wouldn't work:
> 
>      string getTMember(T t, enum string memberName) {
>          return __traits(getMember, t, memberName);
>      }
> 
> My database table is very wide, with many columns. Above ddbc allows a struct
> to map db returned data. Then if I want a member's value to show in vibe.d template,
> how do I use a function to get it?

This works for me:
```D
import std;

void main() {
    struct T {int a; string name;}
     auto t = T(12, "got by member name");
     enum s = "name";
     writeln(__traits(getMember, t, s));
}
```
May 30, 2023
On Tuesday, 30 May 2023 at 08:46:43 UTC, John Xu wrote:
>
> How to put above enum as a function parameter? Following code wouldn't work:
>
>     string getTMember(T t, enum string memberName) {
>         return __traits(getMember, t, memberName);
>     }
>
> ...

As simple as this:
```
string getTMember(T t, string memberName)() {
    return __traits(getMember, t, memberName);
}

...

writeln(getTMember!(t, "..."));
```
May 30, 2023
On Tuesday, 30 May 2023 at 15:24:21 UTC, bauss wrote:
> On Tuesday, 30 May 2023 at 08:46:43 UTC, John Xu wrote:
>>
>> How to put above enum as a function parameter? Following code wouldn't work:
>>
>>     string getTMember(T t, enum string memberName) {
>>         return __traits(getMember, t, memberName);
>>     }
>>
>> ...
>
> As simple as this:
> ```
> string getTMember(T t, string memberName)() {
>     return __traits(getMember, t, memberName);
> }
>
> ...
>
> writeln(getTMember!(t, "..."));
> ```

Noticed a mistake in my code ...

```
getTMember(T t, string memberName)()
```

Should be:

```
getTMember(T, string memberName)(T t)
```
And calling it should be like:

```
writeln(getTMember!("...")(t));
```
May 30, 2023

On 5/30/23 4:46 AM, John Xu wrote:

>

How to put above enum as a function parameter? Following code wouldn't work:

    string getTMember(T t, enum string memberName) {
        return __traits(getMember, t, memberName);
    }

compile time parameters come before runtime parameters:

string getTMember(string memberName)(T t) {
   return __traits(getMember, t, memberName);
}

// used like
auto v = getTMember!"name"(t);

-Steve

May 31, 2023

On Tuesday, 30 May 2023 at 15:43:12 UTC, Steven Schveighoffer wrote:

>

On 5/30/23 4:46 AM, John Xu wrote:

>

How to put above enum as a function parameter? Following code wouldn't work:

    string getTMember(T t, enum string memberName) {
        return __traits(getMember, t, memberName);
    }

compile time parameters come before runtime parameters:

string getTMember(string memberName)(T t) {
   return __traits(getMember, t, memberName);
}

// used like
auto v = getTMember!"name"(t);

-Steve

When render vibe.d diet template,

string[] allMembers = __traits(allMembers, t);
res.render!("index.dt", t, allMembers)

if I don't want write memberName one by one in diet template:

table
    - foreach(memberName; allMembers)
        tr
            td #{memberName}
            td #{getTMember!memberName(t)}

Problem: memberName is not known at compile time.

« First   ‹ Prev
1 2