Re: need help to translate C into D

2022-09-13 Thread test123 via Digitalmars-d-learn

On Tuesday, 13 September 2022 at 23:34:33 UTC, Ali Çehreli wrote:

On 9/13/22 04:07, test123 wrote:
> On Tuesday, 13 September 2022 at 10:59:36 UTC, Dennis wrote:

>> Side node, you can use `immutable` instead of `__gshared
const`, it
>> amounts to the same for global variables.
>
> because __enums_layout.ptr need to be part of other object,
and this
> const ptr cloud be included in multi objects.

There may be valid reasons not to use 'immutable' but you can 
still do what you describe. There is an 'immutable' array below 
and its .ptr is being stored as 'const' inside objects of 
another struct.


struct S {
int i;
string s;
int[3] arr;
}

immutable S[] imm;

shared static this() {
// Initializing immutable at program initialization time:
imm ~= S(42, "hello", [1,2,3]);
imm ~= S(7, "world", [4,5,6]);
}

struct Other {
const S * ptr;
}

void main() {
auto o = Other(imm.ptr);
}

That works because 'const' can point to 'immutable' (and 
mutable and const).


Ali


Thanks for the tips.

This is for a protobuf module and the `int[3] arr;` is variable, 
(diff size struct cloud in same group), and each struct is reused 
in multi part. (and the total number can be big depend on GRPC 
model)


with `shared static this` the size is increased, and there is a 
lot depends relation need resolve during the protobuf init time. 
(the best solution is provide __gshared const relation map so no 
runtime cost, the C code way)



What I can see, the best solution to fix this is allow take 
address of member `__gshared const struct` like this:


```d
struct Header {
  int a;
  int b;
}
struct Message {
 Header header;
 uint[a] arr;
}
__gshared const Message type1 = {{1,2}, [3,4]};
__gshared const header_ptr = 

// then you can reuse header_ptr in multi other __gshared const

```

D dont need change to support `C struct array`, and all relation 
is const safe in memory. (not able to write into this section of 
memory)




Current D support get `__gshared const struct` address, but not 
member of `__gshared const struct` address.  if you do so it will 
throw error:



```sh
expression (3u, 4u).a is not a constant
```


If you can `cast` the member address by the struct.offsetof, then 
this type error you get:


```sh
Error: reinterpreting cast from 
`const(validate_KnownRegex_enum_init_type)*` to 
`const(upb_MiniTable_Enum)*` is not supported in CTFE

```


I can not think of the reason why prevent get const member 
address of `__gshared const struct`.  and can not think of a 
workaround to bypass this problem.















Re: can not take const struct member address at CTFE , is this a bug?

2022-09-13 Thread test123 via Digitalmars-d-learn
On Wednesday, 14 September 2022 at 00:40:38 UTC, Ruby The 
Roobster wrote:
The addresses of items stored in memory are by definition not 
constant.  This isn't a bug.


If so why this can work ?

```d
struct c { uint a, b;}
__gshared const c d = { 3, 4};
__gshared const e = & d;
```

the `e` can get address of `d`, then it should be to get address 
of `d.a`


Re: How to work with long paths on Windows?

2022-09-13 Thread Preetpal via Digitalmars-d-learn

On Tuesday, 13 September 2022 at 19:54:15 UTC, Preetpal wrote:
In Windows 10, Version 1607 (and later), you can [enable long 
paths](https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry) which bypasses the MAX_PATH limitation for local paths (e.g., C:\Users\you\log.txt). Currently if you iterate over a directory with a file exceeding the MAX_PATH limitation for local paths, an exception is thrown. There is no limitation on Linux (tested using GDC on the Windows Subsystem for Linux) and this issue occurs when using either the LDC2 or DMD compilers on Windows. It's very common to have these sorts of paths if you use [npm](https://www.npmjs.com/).


Example code:


```
import std.file;
import std.stdio;

/// Command line tool to find files in directories
int main(string[] args) {
if (args.length < 2) {
writefln("Usage: %s wildcard", args[0]);
return 1;
}
string wildcard = args[1];

auto results = dirEntries(".", wildcard, SpanMode.depth);
foreach(string result; results) {
writeln(result);
}
return 0;
}
```


This issue technically isn't an issue as things are working as 
expected IMO. All you have to do is create an manifest file for 
the executable (this changes the behavior of how the file 
management functions in the Windows API (which are being used in 
std.file) deal with the MAX_PATH restriction). This manifest file 
can either be named exactly the same name as the executable and 
placed in the same directory (in this case if you named the 
program executable "find_file.exe" you would name the manifest 
file "find_file.exe.manifest") or it can be embedded in the 
executable (did not test this myself). The manifest file is just 
an XML file:


```

xmlns="urn:schemas-microsoft-com:asm.v1" 
xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">


xmlns:ws2="http://schemas.microsoft.com/SMI/2016/WindowsSettings;>

  true

xmlns:ws3="http://schemas.microsoft.com/SMI/2019/WindowsSettings;>

  UTF-8

  
  name="YourOrganization.file_name" 
version="1.0.0.7">


```


Example problem:

  If you run it in a directory with paths containing exceeding 
MAX_PATH, it fails.



```
std.file.FileException@std\file.d(4648): 
.\my_webproject\my_webproject\my_webproject\node_modules\bootswatch\docs\3\node_modules\bower\node_modules\update-notifier\node_modules\request\node_modules\har-validator\node_modules\chalk\node_modules\has-ansi\node_modules\ansi-regex: The system cannot find the path specified.

```

How do you work around this issue on Windows? The issue has 
already been 
[reported](https://issues.dlang.org/show_bug.cgi?id=8967).


If you use the manifest file for your executable, you will be 
able to deal with long paths on Windows. By using the manifest 
file, my program (see example code) no longer threw an exception 
when dealing with long paths. I think that the [open 
issue](https://issues.dlang.org/show_bug.cgi?id=8967) should be 
closed as you have to opt-in to make your program long path aware.


Re: can not take const struct member address at CTFE , is this a bug?

2022-09-13 Thread Ruby The Roobster via Digitalmars-d-learn

On Tuesday, 13 September 2022 at 11:16:55 UTC, test123 wrote:

```d
struct c { uint a, b;}
__gshared const c d = { 3, 4};
__gshared const e = 
```

./test.d(4): Error: expression `(3u, 4u).a` is not a constant


I need this to work around C struct array member like this:

```c
struct c {
 uint32_t a, b;
 uint32_t[] arr;
}
```

If I can take const object member address as __gshared const, 
then the problem is fixed.



Or if I can force cast the const object pointer into other 
type, will work be able to work this around. (but not work)


```sh
Error: reinterpreting cast from 
`const(validate_KnownRegex_enum_init_type)*` to 
`const(upb_MiniTable_Enum)*` is not supported in CTFE

```


The addresses of items stored in memory are by definition not 
constant.  This isn't a bug.


Re: How check if destructor has been called?

2022-09-13 Thread Christian Köstlin via Digitalmars-d-learn

On 13.09.22 19:13, Ben Jones wrote:

On Tuesday, 13 September 2022 at 14:06:42 UTC, Injeckt wrote:
Hi, I'm trying to check if destructor has been called, but when I'm 
deleting class object I didn't get any calls from destructor.


myclass.d

    ~this() {
    this.log("\nDestructor\n");
    this._free_trash();
    }


main.d

    try {
    server.server_init(server);
    } catch (Exception e) {
    server.log(e.msg);
    server = null;
    }


Classes are allocated on the GC heap, so even though you're setting the 
reference to null, it's not being collected at that point.  You can use 
`destroy` to make sure the destructor is called (see here: 
https://dlang.org/spec/class.html#destructors)


Or you could make your instance `scope` and then it basically follows 
the same lifetime rules as `struct`s.
Some more things to watch out are mentioned in 
https://p0nce.github.io/d-idioms/#The-trouble-with-class-destructors.


In one of my programs I had problems with allocating in GC called 
destructors (by doing "harmless" writeln debugging).


Kind regards,
Christian




Re: need help to translate C into D

2022-09-13 Thread Ali Çehreli via Digitalmars-d-learn

On 9/13/22 04:07, test123 wrote:
> On Tuesday, 13 September 2022 at 10:59:36 UTC, Dennis wrote:

>> Side node, you can use `immutable` instead of `__gshared const`, it
>> amounts to the same for global variables.
>
> because __enums_layout.ptr need to be part of other object, and this
> const ptr cloud be included in multi objects.

There may be valid reasons not to use 'immutable' but you can still do 
what you describe. There is an 'immutable' array below and its .ptr is 
being stored as 'const' inside objects of another struct.


struct S {
int i;
string s;
int[3] arr;
}

immutable S[] imm;

shared static this() {
// Initializing immutable at program initialization time:
imm ~= S(42, "hello", [1,2,3]);
imm ~= S(7, "world", [4,5,6]);
}

struct Other {
const S * ptr;
}

void main() {
auto o = Other(imm.ptr);
}

That works because 'const' can point to 'immutable' (and mutable and const).

Ali



Re: Function attribute best practices

2022-09-13 Thread Ali Çehreli via Digitalmars-d-learn

On 9/13/22 10:08, Paul Backus wrote:

> Here's my attempt, covering all the attributes found under
> [`MemberFunctionAttribute`][1] in the language spec:
>
> |Attribute|Affects |Inferred?|
> |-||-|
> |nothrow  |Function|Yes  |
> |pure |Function|Yes  |
> |@nogc|Function|Yes  |
> |@safe|Function|Yes  |
> |@system  |Function|Yes  |
> |@trusted |Function|No   |
> |@property|Function|No   |
> |@disable |Function|No   |
> |const|this|No   |
> |immutable|this|No   |
> |inout|this|No   |
> |shared   |this|No   |
> |return   |this|Yes  |
> |scope|this|Yes  |
>
> In general, attributes with a 'Yes' in the 'Inferred?' column should not
> be applied explicitly to functions that are subject to [attribute
> inference][2]. This includes functions defined inside templates, as well
> as nested functions and functions with an inferred return type (i.e.,
> `auto` functions).
>
> [1]: https://dlang.org/spec/function.html#MemberFunctionAttributes
> [2]: https://dlang.org/spec/function.html#function-attribute-inference

That is great! I think we can improve it with guidelines for when to 
write an attribute or not.


For example, carried over from C++, I have this guideline: "put const to 
as many member function as you can." That guideline makes the function 
more useful because I can call it on mutable, const, and immutable 
objects. Great... Programmers can understand that.


Now let's compare it to what the const attribute on a member function 
says (I did not find it in the spec; so making it up): "Makes the 'this' 
reference const." Although correct, it does not help the programmer.


Or... What does pure mean? Does it tell the outside world that the 
function is pure or does it require to be called from pure code? I put 
that here because e.g. 'immutable' on a member function kind of does 
that: It requires to be called on an immutable object. Ok, not a fair 
comparison because there is no "pure object" but still, these are 
thoughts that I think programmers have in mind. (I do. :) )


Ali



How to work with long paths on Windows?

2022-09-13 Thread Preetpal via Digitalmars-d-learn
In Windows 10, Version 1607 (and later), you can [enable long 
paths](https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry) which bypasses the MAX_PATH limitation for local paths (e.g., C:\Users\you\log.txt). Currently if you iterate over a directory with a file exceeding the MAX_PATH limitation for local paths, an exception is thrown. There is no limitation on Linux (tested using GDC on the Windows Subsystem for Linux) and this issue occurs when using either the LDC2 or DMD compilers on Windows. It's very common to have these sorts of paths if you use [npm](https://www.npmjs.com/).


Example code:


```
import std.file;
import std.stdio;

/// Command line tool to find files in directories
int main(string[] args) {
if (args.length < 2) {
writefln("Usage: %s wildcard", args[0]);
return 1;
}
string wildcard = args[1];

auto results = dirEntries(".", wildcard, SpanMode.depth);
foreach(string result; results) {
writeln(result);
}
return 0;
}
```

Example problem:

  If you run it in a directory with paths containing exceeding 
MAX_PATH, it fails.



```
std.file.FileException@std\file.d(4648): 
.\my_webproject\my_webproject\my_webproject\node_modules\bootswatch\docs\3\node_modules\bower\node_modules\update-notifier\node_modules\request\node_modules\har-validator\node_modules\chalk\node_modules\has-ansi\node_modules\ansi-regex: The system cannot find the path specified.

```

How do you work around this issue on Windows? The issue has 
already been 
[reported](https://issues.dlang.org/show_bug.cgi?id=8967).


Re: How check if destructor has been called?

2022-09-13 Thread Ben Jones via Digitalmars-d-learn

On Tuesday, 13 September 2022 at 14:06:42 UTC, Injeckt wrote:
Hi, I'm trying to check if destructor has been called, but when 
I'm deleting class object I didn't get any calls from 
destructor.


myclass.d

~this() {
this.log("\nDestructor\n");
this._free_trash();
}


main.d

try {
server.server_init(server);
} catch (Exception e) {
server.log(e.msg);
server = null;
}


Classes are allocated on the GC heap, so even though you're 
setting the reference to null, it's not being collected at that 
point.  You can use `destroy` to make sure the destructor is 
called (see here: https://dlang.org/spec/class.html#destructors)


Or you could make your instance `scope` and then it basically 
follows the same lifetime rules as `struct`s.


Re: Function attribute best practices

2022-09-13 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 13 September 2022 at 14:16:39 UTC, Ali Çehreli wrote:

On 9/12/22 09:39, Paul Backus wrote:

> Yes. Except for `@trusted`, explicit attributes on template
code are a
> smell.

Except for 'const' as well because some templates are member 
functions. And 'const' on a member function cannot be left to 
inference because it happens to be a part of the type of the 
function, which can be overloaded.


Yes, good point. In my head, I think of attributes that apply to 
the `this` parameter like `const`, `inout`, `shared`, and so as 
being in a separate category from attributes that apply to the 
function itself, like `@safe` and `@trusted`.


Somebody needs to create a two dimensional table that shows 
what it means for each function attribute on a regular 
function, member function, and templates of those, and 
hopefully come up with some guidelines.


Here's my attempt, covering all the attributes found under 
[`MemberFunctionAttribute`][1] in the language spec:


|Attribute|Affects |Inferred?|
|-||-|
|nothrow  |Function|Yes  |
|pure |Function|Yes  |
|@nogc|Function|Yes  |
|@safe|Function|Yes  |
|@system  |Function|Yes  |
|@trusted |Function|No   |
|@property|Function|No   |
|@disable |Function|No   |
|const|this|No   |
|immutable|this|No   |
|inout|this|No   |
|shared   |this|No   |
|return   |this|Yes  |
|scope|this|Yes  |

In general, attributes with a 'Yes' in the 'Inferred?' column 
should not be applied explicitly to functions that are subject to 
[attribute inference][2]. This includes functions defined inside 
templates, as well as nested functions and functions with an 
inferred return type (i.e., `auto` functions).


[1]: https://dlang.org/spec/function.html#MemberFunctionAttributes
[2]: 
https://dlang.org/spec/function.html#function-attribute-inference


Re: Function attribute best practices

2022-09-13 Thread Ali Çehreli via Digitalmars-d-learn

On 9/12/22 09:39, Paul Backus wrote:

> Yes. Except for `@trusted`, explicit attributes on template code are a
> smell.

Except for 'const' as well because some templates are member functions. 
And 'const' on a member function cannot be left to inference because it 
happens to be a part of the type of the function, which can be overloaded.


Somebody needs to create a two dimensional table that shows what it 
means for each function attribute on a regular function, member 
function, and templates of those, and hopefully come up with some 
guidelines.


I started thinking about it but will not have time these coming days. :/

Ali



How check if destructor has been called?

2022-09-13 Thread Injeckt via Digitalmars-d-learn
Hi, I'm trying to check if destructor has been called, but when 
I'm deleting class object I didn't get any calls from destructor.


myclass.d

~this() {
this.log("\nDestructor\n");
this._free_trash();
}


main.d

try {
server.server_init(server);
} catch (Exception e) {
server.log(e.msg);
server = null;
}



Re: Function attribute best practices

2022-09-13 Thread jmh530 via Digitalmars-d-learn

On Monday, 12 September 2022 at 16:39:14 UTC, Paul Backus wrote:

[snip]

Yes. Except for `@trusted`, explicit attributes on template 
code are a smell.


[snip]


If I can be 100% sure that something will always be 
@safe/nothrow/pure/@nogc, then I might consider marking them as 
such. For instance, a function that takes any floating point 
type, does some calculation, and then returns it. I figure it is 
documented for the user and at least this will save the compiler 
the effort of figuring it. If I can't, then I don't.


Re: need help to translate C into D

2022-09-13 Thread test123 via Digitalmars-d-learn

On Tuesday, 13 September 2022 at 11:29:12 UTC, Dennis wrote:

On Tuesday, 13 September 2022 at 11:03:30 UTC, test123 wrote:
and upb_MiniTable_Enum can include a lot diff types. (for 
example mixed diff size upb_MiniTable_Enum)


I think you'll need a `void*` array then, since pointers to 
different structs can all implicitly convert to `void*`.


Thanks for so much tips and help. I will try void*


Re: need help to translate C into D

2022-09-13 Thread Dennis via Digitalmars-d-learn

On Tuesday, 13 September 2022 at 11:03:30 UTC, test123 wrote:
and upb_MiniTable_Enum can include a lot diff types. (for 
example mixed diff size upb_MiniTable_Enum)


I think you'll need a `void*` array then, since pointers to 
different structs can all implicitly convert to `void*`.


can not take const struct member address at CTFE , is this a bug?

2022-09-13 Thread test123 via Digitalmars-d-learn

```d
struct c { uint a, b;}
__gshared const c d = { 3, 4};
__gshared const e = 
```

./test.d(4): Error: expression `(3u, 4u).a` is not a constant


I need this to work around C struct array member like this:

```c
struct c {
 uint32_t a, b;
 uint32_t[] arr;
}
```

If I can take const object member address as __gshared const, 
then the problem is fixed.



Or if I can force cast the const object pointer into other type, 
will work be able to work this around. (but not work)


```sh
Error: reinterpreting cast from 
`const(validate_KnownRegex_enum_init_type)*` to 
`const(upb_MiniTable_Enum)*` is not supported in CTFE

```





Re: need help to translate C into D

2022-09-13 Thread test123 via Digitalmars-d-learn

On Tuesday, 13 September 2022 at 10:59:36 UTC, Dennis wrote:

On Tuesday, 13 September 2022 at 10:45:03 UTC, test123 wrote:
Is there a way to init the __gshared fixed length 
upb_MiniTable_Enum array ?


I don't think so. You could leave your array typed as 
`validate_KnownRegex_enum_init_type` and access it through a 
function that casts it to `upb_MiniTable_Enum`.


Side node, you can use `immutable` instead of `__gshared 
const`, it amounts to the same for global variables.


because __enums_layout.ptr need to be part of other object, and 
this const ptr cloud be included in multi objects.


Re: need help to translate C into D

2022-09-13 Thread test123 via Digitalmars-d-learn

On Tuesday, 13 September 2022 at 10:59:36 UTC, Dennis wrote:

On Tuesday, 13 September 2022 at 10:45:03 UTC, test123 wrote:
Is there a way to init the __gshared fixed length 
upb_MiniTable_Enum array ?


I don't think so. You could leave your array typed as 
`validate_KnownRegex_enum_init_type` and access it through a 
function that casts it to `upb_MiniTable_Enum`.


Side node, you can use `immutable` instead of `__gshared 
const`, it amounts to the same for global variables.



replace with immutable get this error:

```d
static variable `__enums_layout` cannot be read at compile time
```


Re: need help to translate C into D

2022-09-13 Thread test123 via Digitalmars-d-learn

On Tuesday, 13 September 2022 at 10:59:36 UTC, Dennis wrote:

On Tuesday, 13 September 2022 at 10:45:03 UTC, test123 wrote:
Is there a way to init the __gshared fixed length 
upb_MiniTable_Enum array ?


I don't think so. You could leave your array typed as 
`validate_KnownRegex_enum_init_type` and access it through a 
function that casts it to `upb_MiniTable_Enum`.


Side node, you can use `immutable` instead of `__gshared 
const`, it amounts to the same for global variables.


I can not do this. because fixed sized array upb_MiniTable_Enum[] 
is passed into other const object. and upb_MiniTable_Enum can 
include a lot diff types. (for example mixed diff size 
upb_MiniTable_Enum)


Re: need help to translate C into D

2022-09-13 Thread Dennis via Digitalmars-d-learn

On Tuesday, 13 September 2022 at 10:45:03 UTC, test123 wrote:
Is there a way to init the __gshared fixed length 
upb_MiniTable_Enum array ?


I don't think so. You could leave your array typed as 
`validate_KnownRegex_enum_init_type` and access it through a 
function that casts it to `upb_MiniTable_Enum`.


Side node, you can use `immutable` instead of `__gshared const`, 
it amounts to the same for global variables.


Re: need help to translate C into D

2022-09-13 Thread test123 via Digitalmars-d-learn

On Tuesday, 13 September 2022 at 10:45:03 UTC, test123 wrote:

upb_MiniTable_Enum array ?


2 type error I think it cloud be compiler bugs.


1): `expression `_KnownRegex_enum_init_type(64u, 2u, 
[7u, 0u], ).header` is not a constant`


```d
union validate_KnownRegex_enum_init_type { struct {uint a, b; 
uint[2] data;} upb_MiniTable_Enum header; }
__gshared const validate_KnownRegex_enum_init_type 
validate_KnownRegex_enum_init = { 64, 2, [7, 0] };

__gshared const upb_MiniTable_Enum*[1] __enums_layout = [
_KnownRegex_enum_init.header,
];
```

2:) `Error: reinterpreting cast from 
`const(validate_KnownRegex_enum_init_type)*` to 
`const(upb_MiniTable_Enum)*` is not supported in CTFE`


```d
struct validate_KnownRegex_enum_init_type { uint a, b; 
uint[2] data;}
__gshared const validate_KnownRegex_enum_init_type 
validate_KnownRegex_enum_init = { 64, 2, [7, 0] };

__gshared const upb_MiniTable_Enum*[1] __enums_layout = [
cast(const upb_MiniTable_Enum*) 
_KnownRegex_enum_init,

];
```


Re: need help to translate C into D

2022-09-13 Thread test123 via Digitalmars-d-learn

On Tuesday, 13 September 2022 at 09:57:38 UTC, Dennis wrote:

On Tuesday, 13 September 2022 at 09:43:46 UTC, test123 wrote:

This will not work since the C have no array like D.


You can use a 0-size static array:
```D
struct mystruct {
	uint32_t mask_limit;   // Limit enum value that can be tested 
with mask.

uint32_t value_count;  // Number of values after the bitfield.
uint32_t[0] data;  // Bitmask + enumerated values follow.
}
```

Then you have to index with `mystructVar.data.ptr[i]` to avoid 
bounds checking.


Thanks, this look nice.

What I need is init the const object at CTFE.

I try this way:

```d
struct validate_KnownRegex_enum_init_type { const 
upb_MiniTable_Enum header; uint[2] data; }
__gshared const validate_KnownRegex_enum_init_type 
validate_KnownRegex_enum_init = { {64, 2}, [7, 0] };

```

The problem is I can not assign it into one array (give error):

```d
__gshared const test = cast(upb_MiniTable_Enum*) 
_KnownRegex_enum_init;

__gshared const __enums_layout = [
test,
];
```

This will also not work:

```d
__gshared const upb_MiniTable_Enum*[1] __enums_layout = [
_KnownRegex_enum_init.header,
];
```

Is there a way to init the __gshared fixed length 
upb_MiniTable_Enum array ?




Re: need help to translate C into D

2022-09-13 Thread Dennis via Digitalmars-d-learn

On Tuesday, 13 September 2022 at 09:43:46 UTC, test123 wrote:

This will not work since the C have no array like D.


You can use a 0-size static array:
```D
struct mystruct {
	uint32_t mask_limit;   // Limit enum value that can be tested 
with mask.

uint32_t value_count;  // Number of values after the bitfield.
uint32_t[0] data;  // Bitmask + enumerated values follow.
}
```

Then you have to index with `mystructVar.data.ptr[i]` to avoid 
bounds checking.


Re: need help to translate C into D

2022-09-13 Thread test123 via Digitalmars-d-learn

On Tuesday, 13 September 2022 at 07:31:50 UTC, Marvin wrote:

On Tuesday, 13 September 2022 at 06:04:49 UTC, test123 wrote:

I can not use importC, I need it to be work in D code.


```d
typedef struct {
  uint32_t mask_limit;   // Limit enum value that can be 
tested with mask.
  uint32_t value_count;  // Number of values after the 
bitfield.

  uint32_t data[];   // Bitmask + enumerated values follow.
} upb_MiniTable_Enum;
```

I am not sure how to define the `__gshared const 
upb_MiniTable_Enum` object for this struct.



struct mystruct {
  uint32_t mask_limit;   // Limit enum value that can be 
tested with mask.
  uint32_t value_count;  // Number of values after the 
bitfield.
  uint32_t[] data;   // Bitmask + enumerated values 
follow.

} __gshared mystruct upb_MiniTable_Enum;



This will not work since the C have no array like D.


Re: Linker Error with Template Function

2022-09-13 Thread Nick Treleaven via Digitalmars-d-learn
On Tuesday, 13 September 2022 at 03:00:17 UTC, Kyle Ingraham 
wrote:
Any suggestions for being able to call one function for any 
instance given but maintain flexible return types?


Not sure if it helps, but you can define final methods in an 
interface, which can call virtual interface methods:

```d
interface PathConverter
{
string getValue();

final T toD(T)()
{
import std.conv : to;

return to!T(getValue());
}
}
```
Not tested as AFK.


Re: need help to translate C into D

2022-09-13 Thread Marvin via Digitalmars-d-learn

On Tuesday, 13 September 2022 at 06:04:49 UTC, test123 wrote:

I can not use importC, I need it to be work in D code.


```d
typedef struct {
  uint32_t mask_limit;   // Limit enum value that can be tested 
with mask.

  uint32_t value_count;  // Number of values after the bitfield.
  uint32_t data[];   // Bitmask + enumerated values follow.
} upb_MiniTable_Enum;
```

I am not sure how to define the `__gshared const 
upb_MiniTable_Enum` object for this struct.



struct mystruct {
  uint32_t mask_limit;   // Limit enum value that can be 
tested with mask.
  uint32_t value_count;  // Number of values after the 
bitfield.
  uint32_t[] data;   // Bitmask + enumerated values 
follow.

} __gshared mystruct upb_MiniTable_Enum;


Re: Using .require for struct types

2022-09-13 Thread Erdem via Digitalmars-d-learn

On Sunday, 11 September 2022 at 21:01:27 UTC, Salih Dincer wrote:
On Saturday, 10 September 2022 at 16:33:03 UTC, Erdem Demir 
wrote:
I wish I could use ref DListOfA returnVal =  but we can't 
in D.


Can you please suggest alternatives?


I think you should try advanced update. Your flexibility and 
what you can do are limited by your dreams. A couple delicious 
code:


```d
import object, std.container;

struct A
{
double val;
bool isBig;
}

void main()
{
    alias  DListOfA = DList!A;
    DListOfA returnVal;
    //DListOfA[string] temp;/*
    DListOfA[string] temp = [
        "a": DListOfA( A(0) )
    ];//*/
    
    auto a = A(6, true); // replacement element
    temp.update("a", {
        return DListOfA( A(0) ); // not updated: unsucceeded 
but initialized

    }, (ref DListOfA v) {
        v = DListOfA( a ); // existing v has been replaced
        returnVal = v;
assert(returnVal.front == a);
    });
    assert(is(typeof(temp["a"]) == DList!A));
}
```
SDB@79


I really like the possibilities of update as well but the 
overhead is too much especially in this example. I am sorry to 
say I found old "in" usage less complicated than that even I 
think "in" is complicated. I liked the Steven's solution all this 
code can be done via 2 lines only.





Re: Using .require for struct types

2022-09-13 Thread Erdem via Digitalmars-d-learn
On Saturday, 10 September 2022 at 18:39:55 UTC, Steven 
Schveighoffer wrote:

On 9/10/22 12:33 PM, Erdem Demir wrote:


Can you please suggest alternatives?



Use a pointer.

```d
DListOfA *returnVal = (...);
returnVal.insert(a);
```

-Steve


Actually that could be answer I am seeking for I will try it.


Re: Using .require for struct types

2022-09-13 Thread Erdem via Digitalmars-d-learn

On Saturday, 10 September 2022 at 18:38:40 UTC, Ali Çehreli wrote:

On 9/10/22 09:33, Erdem Demir wrote:

>  DListOfA returnVal = temp.require("a",
DListOfA());--> I wish I
> could use ref DListOfA here

But keeping a reference to a temporary would not work because 
the life of that temporary ends by the end of that expression 
(practically, at the semicolon).


An option is to allocate the object dynamically with new (and 
store DListOfA* in the associative array). Then the GC would 
keep it alive as long its pointer was in the associative arrray.


But a better option is to just forget about it because D 
already takes care of rvalues by blitting (bit-level copying) 
them by default. Everything just works... :) It is not 
expensive either. For example, your struct is very cheap to 
copy.


But I am probably missing the reason why you want a ref there. 
Perhaps there are even better options.


Ali


In the code sample I posted I need returnVal.insert(a); have an 
effect on DListOfA[string] temp; but if returnVal is a rvalue 
copy I can't accomplish this right?


I see your point about ref would have been assign to a temp val, 
thanks for pointing out.





need help to translate C into D

2022-09-13 Thread test123 via Digitalmars-d-learn

I can not use importC, I need it to be work in D code.


```d
typedef struct {
  uint32_t mask_limit;   // Limit enum value that can be tested 
with mask.

  uint32_t value_count;  // Number of values after the bitfield.
  uint32_t data[];   // Bitmask + enumerated values follow.
} upb_MiniTable_Enum;
```

I am not sure how to define the `__gshared const 
upb_MiniTable_Enum` object for this struct.