Re: How get struct value by member name string ?

2023-05-30 Thread John Xu via Digitalmars-d-learn
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:

```d
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.


Re: Code duplication where you wish to have a routine called with either immutable or mutable arguments

2023-05-30 Thread Cecil Ward via Digitalmars-d-learn

On Wednesday, 31 May 2023 at 03:23:01 UTC, Cecil Ward wrote:

On Tuesday, 30 May 2023 at 04:15:22 UTC, Ali Çehreli wrote:

On 5/29/23 19:57, Cecil Ward wrote:

> I wish to have one routine
> that can be called with either immutable or (possibly)
mutable argument
> values.

'const' should take both immutable and mutable. Can you show 
your case with a short example?


> Could I make the one routine into a template?

That could work but give 'in' parameters a try:

  https://dlang.org/spec/function.html#in-params

Ali


T2 foo( in T1 x ) { return bar( x ) };

It was with something vaguely like the above that I had to 
remove the in (templatised generic function possibly) in order 
to get it to compile with GDC or LDC on godbolt.org (or 
d.godbolt.org) latest versions available. -O3 
-release/-frelease -march=native/-mcpu-native


I have to admit that I don’t really understand immutable. I have 
an idea that it could mean that an object has an address in ROM, 
so its value will never change. Maybe const doesn’t give you such 
a strong guarantee, disallows ‘you’ from modifying it but others 
might do so, but who knows. Without a guarantee as strong as the 
first idea I can’t really understand how const can work properly. 
"You treat it as const so do not modify it, but it might not be 
eternally fixed and  unchanging" that doesn’t seem to have enough 
value to me. But maybe I’ve got the whole thing wrong.


In an architecture where you have strongly typed (tagged ? 
segmented?) different kinds of addresses, I can see why you might 
be getting type mismatch errors when passing addresses around.


Re: Code duplication where you wish to have a routine called with either immutable or mutable arguments

2023-05-30 Thread Cecil Ward via Digitalmars-d-learn

On Tuesday, 30 May 2023 at 04:15:22 UTC, Ali Çehreli wrote:

On 5/29/23 19:57, Cecil Ward wrote:

> I wish to have one routine
> that can be called with either immutable or (possibly)
mutable argument
> values.

'const' should take both immutable and mutable. Can you show 
your case with a short example?


> Could I make the one routine into a template?

That could work but give 'in' parameters a try:

  https://dlang.org/spec/function.html#in-params

Ali


T2 foo( in T1 x ) { return bar( x ) };

It was with something vaguely like the above that I had to remove 
the in (templatised generic function possibly) in order to get it 
to compile with GDC or LDC on godbolt.org (or d.godbolt.org) 
latest versions available. -O3 -release/-frelease 
-march=native/-mcpu-native





Re: How get struct value by member name string ?

2023-05-30 Thread Steven Schveighoffer via Digitalmars-d-learn

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:

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

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

-Steve


Re: Code duplication where you wish to have a routine called with either immutable or mutable arguments

2023-05-30 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/29/23 10:57 PM, Cecil Ward wrote:
I have often come into difficulties where I wish to have one routine 
that can be called with either immutable or (possibly) mutable argument 
values. The argument(s) in question are in, readonly, passed by value or 
passed by const reference. Anyway, no one is trying to write to the 
items passed in as args, no badness attempted.


In cases where const doesn't suffice, inout might. inout has a feature 
that it can be used in cases where a const nested behind multiple 
references would not work.


-Steve


Re: How get struct value by member name string ?

2023-05-30 Thread bauss via Digitalmars-d-learn

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));
```


Re: How get struct value by member name string ?

2023-05-30 Thread bauss via Digitalmars-d-learn

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, "..."));
```


Re: How get struct value by member name string ?

2023-05-30 Thread drug007 via Digitalmars-d-learn

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));
}
```


Re: SumType! seemingly results in cryptic error, with dub suggesting to make an issue ticket

2023-05-30 Thread Gimbles via Digitalmars-d-learn

On Tuesday, 30 May 2023 at 08:54:33 UTC, Gimbles wrote:

On Tuesday, 30 May 2023 at 08:47:16 UTC, user456 wrote:

On Tuesday, 30 May 2023 at 08:42:38 UTC, Gimbles wrote:

My code is this
[...]
Should I make an issue for this as it suggests?


100% yes. This an "Internal Compiler Error" (ICE), meaning the 
compiler has crashed.
Open a ticket here : 
https://issues.dlang.org/show_bug.cgi?id=23935


with a title like "ICE caused by std.sumtype", and paste the 
test case.


Alright, thanks! Opening a ticket :)


https://issues.dlang.org/show_bug.cgi?id=23945

For reference ^^^


Re: SumType! seemingly results in cryptic error, with dub suggesting to make an issue ticket

2023-05-30 Thread Gimbles via Digitalmars-d-learn

On Tuesday, 30 May 2023 at 08:47:16 UTC, user456 wrote:

On Tuesday, 30 May 2023 at 08:42:38 UTC, Gimbles wrote:

My code is this
[...]
Should I make an issue for this as it suggests?


100% yes. This an "Internal Compiler Error" (ICE), meaning the 
compiler has crashed.
Open a ticket here : 
https://issues.dlang.org/show_bug.cgi?id=23935


with a title like "ICE caused by std.sumtype", and paste the 
test case.


Alright, thanks! Opening a ticket :)


Re: SumType! seemingly results in cryptic error, with dub suggesting to make an issue ticket

2023-05-30 Thread user456 via Digitalmars-d-learn

On Tuesday, 30 May 2023 at 08:42:38 UTC, Gimbles wrote:

My code is this
[...]
Should I make an issue for this as it suggests?


100% yes. This an "Internal Compiler Error" (ICE), meaning the 
compiler has crashed.
Open a ticket here : 
https://issues.dlang.org/show_bug.cgi?id=23935


with a title like "ICE caused by std.sumtype", and paste the test 
case.


Re: SumType! seemingly results in cryptic error, with dub suggesting to make an issue ticket

2023-05-30 Thread Gimbles via Digitalmars-d-learn

On Tuesday, 30 May 2023 at 08:42:38 UTC, Gimbles wrote:

My code is this
```d
struct ConstValueIndex
{
ushort const_value_index;
}

[...]


https://gist.github.com/run-dlang/5dd783c750f04329405af1b1e4a83cde

Here's the full source


Re: How get struct value by member name string ?

2023-05-30 Thread John Xu via Digitalmars-d-learn

On Tuesday, 30 May 2023 at 01:33:54 UTC, H. S. Teoh wrote:
On Tue, May 30, 2023 at 01:24:46AM +, 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?


SumType! seemingly results in cryptic error, with dub suggesting to make an issue ticket

2023-05-30 Thread Gimbles via Digitalmars-d-learn

My code is this
```d
struct ConstValueIndex
{
ushort const_value_index;
}

struct EnumConstValue
{
ushort type_name_index;
ushort const_name_index;
}

struct ClassInfoIndex
{
ushort class_info_index;
}

struct AnnotationValue
{
Annotation annotation_value;
}

struct ArrayValue
{
ElementValue[] values;
}

alias ElementValue = SumType!(
ConstValueIndex,
EnumConstValue,
ClassInfoIndex,
AnnotationValue,
ArrayValue,
);
```

It results in quite a cryptic error
```d
Error: unknown, please file report on issues.dlang.org
/usr/include/dlang/dmd/std/meta.d(632,42): Error: template 
instance `std.sumtype.matchImpl!(Flag.yes, 
destroyIfOwner).matchImpl!(SumType!(ConstValueIndex, 
EnumConstValue, ClassInfoIndex, AnnotationValue, 
ArrayValue)).matchImpl.valueTypes!4LU.fun!0LU` error instantiating
/usr/include/dlang/dmd/std/sumtype.d(1931,32):
instantiated from here: `Map!(getType, 0LU)`
/usr/include/dlang/dmd/std/sumtype.d(1967,51):
instantiated from here: `valueTypes!4LU`
/usr/include/dlang/dmd/std/sumtype.d(1649,52):
instantiated from here: `matchImpl!(SumType!(ConstValueIndex, 
EnumConstValue, ClassInfoIndex, AnnotationValue, ArrayValue))`
/usr/include/dlang/dmd/std/sumtype.d(752,17):instantiated 
from here: `match!(SumType!(ConstValueIndex, EnumConstValue, 
ClassInfoIndex, AnnotationValue, ArrayValue))`
source/parser/definitions/attributes.d(262,22):
instantiated from here: `SumType!(ConstValueIndex, 
EnumConstValue, ClassInfoIndex, AnnotationValue, ArrayValue)`
/usr/include/dlang/dmd/std/sumtype.d(1985,13):while 
evaluating: `static assert((__error)(hid))`

Error /usr/bin/dmd failed with exit code 1.
```

(I tried all three compilers, happens in all)

Should I make an issue for this as it suggests?