Re: Is `void` the correct way to say "do not initialize this variable"?

2022-10-02 Thread drug007 via Digitalmars-d-learn

On 10/3/22 02:30, ryuukk_ wrote:
I have tried to look at the documentation and various places on the DMD 
source, but i couldn't find the answer


https://dlang.org/spec/declaration.html#void_init



```D
MyStruct test = void;
```

Does this guarantee that the compiler will not initialize it?


Yes



Does it work with static arrays of struct too?



It works but not as someone could expect. In case of
```
Foo[2] arr = void;
```
`arr` value is not defined, it is not an initialized array of 
uninitialized elements like you want, it is just uninitialized array.



The generated code is different than ``MyStruct test;``

What exactly (by exactly i mean is the behavior documented somewhere?) 
``void`` does?








Re: Is `void` the correct way to say "do not initialize this variable"?

2022-10-02 Thread ryuukk_ via Digitalmars-d-learn
I got the answer thanks to IRC chat: 
https://dlang.org/spec/declaration.html#void_init


Is `void` the correct way to say "do not initialize this variable"?

2022-10-02 Thread ryuukk_ via Digitalmars-d-learn
I have tried to look at the documentation and various places on 
the DMD source, but i couldn't find the answer


```D
MyStruct test = void;
```

Does this guarantee that the compiler will not initialize it?

Does it work with static arrays of struct too?

The generated code is different than ``MyStruct test;``

What exactly (by exactly i mean is the behavior documented 
somewhere?) ``void`` does?




Re: csvReader: how to read only selected columns while the class Layout has extra field?

2022-10-02 Thread rassoc via Digitalmars-d-learn

On 10/2/22 23:18, mw via Digitalmars-d-learn wrote:

A CSV library should consider all the use cases, and allow users to ignore 
certain fields.


Filed issue: https://issues.dlang.org/show_bug.cgi?id=23383

Let's see what others have to say.


Re: Visual D doesn't work, now Visual Studio Code / D doesn't work!!!! ....

2022-10-02 Thread rikki cattermole via Digitalmars-d-learn
Visual Studio with its c++ components can debug D code, it should not 
require Visual D to do so.


Open executable as project.

If this does not work, you have a serious issue in your system/VS install.

This may help to narrow down what is going on.



Re: csvReader: how to read only selected columns while the class Layout has extra field?

2022-10-02 Thread mw via Digitalmars-d-learn

On Sunday, 2 October 2022 at 21:03:40 UTC, rassoc wrote:

But say, I'm curious, what's the purpose of adding an 
optional/useless contents field? What's the use-case here?



We have a class/struct for a data record,
some of its data fields need to be saved/loaded from CSV files; 
while there are other helper fields which are useful for various 
computation tasks (e.g. caching some intermediate computation 
results), these fields do not need to be saved/loaded from the 
csv files.



A CSV library should consider all the use cases, and allow users 
to ignore certain fields.


Re: Stop writeln from calling object destructor

2022-10-02 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 2 October 2022 at 18:24:51 UTC, Ali Çehreli wrote:

On 10/2/22 10:55, data pulverizer wrote:
> ```
> this(T)(ref return scope T original)
> if(is(T == RVector!(Type)))
> {
>  //... code ...
> }
> ```

I've just tested. That is used only for explicit constructor 
syntax:


auto b = RVector!int(a);// templatized

>
>
> But this now works:
>
>
> ```
> this(ref return scope RVector!(Type) original)
> {
>  //... code ...
> }
> ```

That one works for both syntaxes:

auto b = RVector!int(a);// templatized
auto c = a; // non-templatized

Certainly confusing and potentially a bug... :/


It's a bug in the documentation.

https://issues.dlang.org/show_bug.cgi?id=23382
https://github.com/dlang/dlang.org/pull/3427


Re: csvReader: how to read only selected columns while the class Layout has extra field?

2022-10-02 Thread rassoc via Digitalmars-d-learn

On 10/2/22 21:48, mw via Digitalmars-d-learn wrote:

```
     text.csvReader!Layout(["b","c","a"]);  // Read only these column
```

The intention is very clear: only read the selected columns from the csv, and 
for any other fields of class Layout, just ignore (with the default D .init 
value).



Here's why it's not currently working:

"An optional header can be provided. The first record will be read in as the header. 
If Contents is a struct then the header provided is expected to correspond to the fields 
in the struct."

"expected to correspond" means that the number of fields in the content struct 
can't exceed the header element count as you can see in the actual code [1]:

```
foreach (ti, ToType; Fields!(Contents))
{
if (indices[ti] == colIndex) // indices.length depends on passed in 
colHeaders.length
...
}
```

The current index exception is bad, this needs an assert in the constructor 
with a nicer error message.

But say, I'm curious, what's the purpose of adding an optional/useless contents 
field? What's the use-case here?

[1] 
https://github.com/dlang/phobos/blob/8e8aaae5080ccc2e0a2202cbe9778dca96496a95/std/csv.d#L1209


Re: csvReader: how to read only selected columns while the class Layout has extra field?

2022-10-02 Thread mw via Digitalmars-d-learn

```
text.csvReader!Layout(["b","c","a"]);  // Read only these 
column

```

The intention is very clear: only read the selected columns from 
the csv, and for any other fields of class Layout, just ignore 
(with the default D .init value).




csvReader: how to read only selected columns while the class Layout has extra field?

2022-10-02 Thread mw via Digitalmars-d-learn

Hi,

I'm following the example on

https://dlang.org/phobos/std_csv.html

```
class Layout
{
int value;
double other;
string name;
int extra_field;  // un-comment to see the error
}

void main()
{
import std.csv;
import std.stdio: write, writeln, writef, writefln;
import std.algorithm.comparison : equal;
string text = "a,b,c\nHello,65,2.5\nWorld,123,7.5";

auto records =
text.csvReader!Layout(["b","c","a"]);  // Read only these 
column

foreach (r; records) writeln(r.name);
}
```

This works fine so far, but if I un-comment the extra_field line, 
I got runtime error:


```
core.exception.ArrayIndexError@/dlang/dmd/linux/bin64/../../src/phobos/std/csv.d(1209):
 index [3] is out of bounds for array of length 3

??:? _d_arraybounds_indexp [0x5565b4b974d1]
/dlang/dmd/linux/bin64/../../src/phobos/std/csv.d:1209 pure @safe 
void std.csv.CsvReader!(onlineapp.Layout, 1, immutable(char)[], 
dchar, immutable(char)[][]).CsvReader.prime() [0x5565b4b73ed2]
/dlang/dmd/linux/bin64/../../src/phobos/std/csv.d:1154 pure @safe 
void std.csv.CsvReader!(onlineapp.Layout, 1, immutable(char)[], 
dchar, immutable(char)[][]).CsvReader.popFront() [0x5565b4b73c80]
/dlang/dmd/linux/bin64/../../src/phobos/std/csv.d:1069 pure ref 
@safe std.csv.CsvReader!(onlineapp.Layout, 1, immutable(char)[], 
dchar, immutable(char)[][]).CsvReader 
std.csv.CsvReader!(onlineapp.Layout, 1, immutable(char)[], dchar, 
immutable(char)[][]).CsvReader.__ctor(immutable(char)[], 
immutable(char)[][], dchar, dchar, bool) [0x5565b4b73ae8]
/dlang/dmd/linux/bin64/../../src/phobos/std/csv.d:366 pure @safe 
std.csv.CsvReader!(onlineapp.Layout, 1, immutable(char)[], dchar, 
immutable(char)[][]).CsvReader 
std.csv.csvReader!(onlineapp.Layout, 1, immutable(char)[], 
immutable(char)[][], char).csvReader(immutable(char)[], 
immutable(char)[][], char, char, bool) [0x5565b4b735f3]

./onlineapp.d:18 _Dmain [0x5565b4b72ca4]
```

I'm just wondering how to work-around this?

Thanks.


Re: Stop writeln from calling object destructor

2022-10-02 Thread data pulverizer via Digitalmars-d-learn

On Sunday, 2 October 2022 at 18:24:51 UTC, Ali Çehreli wrote:
I've just tested. That is used only for explicit constructor 
syntax ...


Many thanks. Knowledgeable as always!



Re: Stop writeln from calling object destructor

2022-10-02 Thread Ali Çehreli via Digitalmars-d-learn

On 10/2/22 10:55, data pulverizer wrote:
> On Sunday, 2 October 2022 at 17:28:51 UTC, data pulverizer wrote:
>> Sorry I'll need to implement all the overloaded copy constructors and
>> see if that fixes it.
>
> I've got it, something weird happened to my copy constructor. This was
> my original attempt and was ignored (didn't run in the copy constructor):
>
> ```
> this(T)(ref return scope T original)
> if(is(T == RVector!(Type)))
> {
>  //... code ...
> }
> ```

I've just tested. That is used only for explicit constructor syntax:

auto b = RVector!int(a);// templatized

>
>
> But this now works:
>
>
> ```
> this(ref return scope RVector!(Type) original)
> {
>  //... code ...
> }
> ```

That one works for both syntaxes:

auto b = RVector!int(a);// templatized
auto c = a; // non-templatized

Certainly confusing and potentially a bug... :/

> No idea why. `Type` is a template parameter of the object.

Minor convenience: You can replace all RVector!(Type) with just RVector 
in the implementation of RVector because the name of the struct template 
*is* that specific instantiation of it:


struct RVector(Type) {
// RVector below means RVector!Type:
this(ref return scope RVector original)
{
// ...
}
}

Ali




Re: Stop writeln from calling object destructor

2022-10-02 Thread data pulverizer via Digitalmars-d-learn

On Sunday, 2 October 2022 at 17:51:59 UTC, Ali Çehreli wrote:
What I noticed first in your original code was that it would be 
considered buggy because it was not considering copying. Every 
struct that does something in its destructor should either have 
post-blit (or copy constructor) defined or simpler, disallow 
copying altogether.


Thanks for the advice, for a while now I didn't know what was 
creating the issue.


The code I'm running is my D connector to the R API and for ages 
I didn't know where the multiple destructor calls to allow an 
object to be garbage collected by the R API was coming from, and 
it was breaking the whole thing.


I think I'll have to play it by ear whether to disable the copy 
constructor altogether or to use it now it is working.


Thanks both of you.



Re: Stop writeln from calling object destructor

2022-10-02 Thread data pulverizer via Digitalmars-d-learn

On Sunday, 2 October 2022 at 17:28:51 UTC, data pulverizer wrote:
Sorry I'll need to implement all the overloaded copy 
constructors and see if that fixes it.


I've got it, something weird happened to my copy constructor. 
This was my original attempt and was ignored (didn't run in the 
copy constructor):


```
this(T)(ref return scope T original)
if(is(T == RVector!(Type)))
{
//... code ...
}
```


But this now works:


```
this(ref return scope RVector!(Type) original)
{
//... code ...
}
```

No idea why. `Type` is a template parameter of the object.


Re: Stop writeln from calling object destructor

2022-10-02 Thread Ali Çehreli via Digitalmars-d-learn

On 10/2/22 10:28, data pulverizer wrote:
> On Sunday, 2 October 2022 at 17:19:55 UTC, data pulverizer wrote:
>> Any reason why this could be?
>

What I noticed first in your original code was that it would be 
considered buggy because it was not considering copying. Every struct 
that does something in its destructor should either have post-blit (or 
copy constructor) defined or simpler, disallow copying altogether.


That's what I did here:

  https://github.com/acehreli/alid/blob/main/cached/alid/cached.d#L178

@disable this(this);

I think disabling copy constructor was unnecessary but I did that as well:

@disable this(ref const(typeof(this)));

The issue remains and bothers me as well. I think writeln copies objects 
because D disallows references to rvalue. We couldn't print rvalues if 
writeln insisted on 'ref'. Or, rvalues would be copied anyway if we used 
'auto ref'. Hence the status quo...


> Sorry I'll need to implement all the overloaded copy constructors and
> see if that fixes it.

The best solution I know is to disable copying and printing not the 
object but an explicit string representation of it:


Added:

@disable this(this);

Added (there are better ways of doing the same e.g. using a 'sink' 
parameter):


string toString() const {
import std.format : format;
return format!"id: %s"(id);
}

Called toString:

writeln("MyObject: ", obj.toString);

Ali




Re: Stop writeln from calling object destructor

2022-10-02 Thread data pulverizer via Digitalmars-d-learn

On Sunday, 2 October 2022 at 17:19:55 UTC, data pulverizer wrote:

Any reason why this could be?


Sorry I'll need to implement all the overloaded copy constructors 
and see if that fixes it.


Re: Stop writeln from calling object destructor

2022-10-02 Thread data pulverizer via Digitalmars-d-learn

On Sunday, 2 October 2022 at 16:44:25 UTC, Paul Backus wrote:
It's because `writeln` is copying the object, and each of the 
copies is being destroyed. If you add a copy constructor to 
your example, you can see it happening:

...


I thought something like this could be happening in my original 
implementation and tried implementing a copy constructor using 
this reference 
https://dlang.org/spec/struct.html#struct-copy-constructor but it 
did not work. Both your's and the manual's suggestion works for 
my baby example but not for my actual code.


Any reason why this could be?



Re: Stop writeln from calling object destructor

2022-10-02 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 2 October 2022 at 16:21:47 UTC, data pulverizer wrote:
I've noticed that `writeln` calls the destructor of a struct 
multiple times and would like to know how to stop this from 
happening.


It's because `writeln` is copying the object, and each of the 
copies is being destroyed. If you add a copy constructor to your 
example, you can see it happening:


```d
import std.stdio: writeln;

struct MyObject
{
int id;
this(int id) @nogc
{
this.id = id;
}
this(inout ref MyObject) inout
{
writeln("Object copy constructor...");
}
~this()
{
writeln("Object destructor ...");
}
}



void main()
{
auto obj = MyObject(42);
writeln(obj);
writeln("Goodbye:\n");
}
```

Output:

```d
Object copy constructor...
Object copy constructor...
Object copy constructor...
Object copy constructor...
MyObject(0)Object destructor ...
Object destructor ...

Object destructor ...
Object destructor ...
Goodbye:

Object destructor ...
```


Stop writeln from calling object destructor

2022-10-02 Thread data pulverizer via Digitalmars-d-learn
I've noticed that `writeln` calls the destructor of a struct 
multiple times and would like to know how to stop this from 
happening. It has become a very serious problem when working with 
objects that have memory management external to D.


Here is a repeatable example, where the destructor appears to 
have been called 4 times with one call of `writeln` before the 
object actually goes out of scope:



Code:
```
import std.stdio: writeln;

struct MyObject
{
int id;
this(int id) @nogc
{
this.id = id;
}
~this()
{
writeln("Object destructor ...");
}
}



void main()
{
auto obj = MyObject(42);
writeln("MyObject: ", obj);
writeln("Goodbye:\n");
}

```

Output:
```
$ rdmd gc.d
MyObject: MyObject(42)Object destructor ...
Object destructor ...

Object destructor ...
Object destructor ...
Goodbye:

Object destructor ...
```

Thank you


Re: D installer

2022-10-02 Thread Imperatorn via Digitalmars-d-learn

On Sunday, 2 October 2022 at 12:37:37 UTC, Mike Parker wrote:

On Sunday, 2 October 2022 at 11:33:47 UTC, Imperatorn wrote:
I only have Visual Studio 2022. Will the installer be updated 
to support that or am I missing some components?


![Installer](https://i.ibb.co/sCZRFRf/installer.jpg)



You should be fine. Select the bottom option since you already 
have it installed.


Thanks for a quick reply!


Re: Visual D doesn't work, now Visual Studio Code / D doesn't work!!!! ....

2022-10-02 Thread Mike Parker via Digitalmars-d-learn
On Sunday, 2 October 2022 at 11:00:06 UTC, Daniel Donnell, Jr 
wrote:

I thought I set everything up correctly, and now:

```
Exception thrown at 0x7FF7D6E2E230 in metamath-d.exe: 
0xC096: Privileged instruction.
Unable to open natvis file 
'c:\Users\fruit\.vscode\extensions\webfreak.code-d-0.23.2\dlang-debug\dlang_cpp.natvis'.

```

So what the hell do you D developers use to code with if A) 
Visual D doesn't work - it just ate my app.obj file and can't 
find it anymore no matter if I clean or re-order the executable 
paths in settings.


B) VS Code doesn't work out-of-the-box.

Every. Single. Time I get around to using D, there's always 
something that pops up and doesn't work right.  And it never 
gets fixed.   How the heck do you expect me to debug without a 
proper debugging IDE :|


Plenty of people are using Visual D and VS Code with D. Whatever 
the source of your problem, it's surely fixable. Though if no one 
has encountered this particular issue, then it may take some 
doing to figure out the problem.


I've emailed Rainer, the maintainer of Visual D, to notify him of 
this thread. He might have an idea of what's wrong, or at least 
will be in a position to ask more informed questions than I or 
others to help you solve the problem.


In the future, when you encounter Visual D issues, please post in 
the IDEs forum. Rainer checks in there more regularly and will be 
more likely to see your posts.


https://forum.dlang.org/group/ide


Re: D installer

2022-10-02 Thread Mike Parker via Digitalmars-d-learn

On Sunday, 2 October 2022 at 11:33:47 UTC, Imperatorn wrote:
I only have Visual Studio 2022. Will the installer be updated 
to support that or am I missing some components?


![Installer](https://i.ibb.co/sCZRFRf/installer.jpg)



You should be fine. Select the bottom option since you already 
have it installed.




Re: Visual D doesn't work, now Visual Studio Code / D doesn't work!!!! ....

2022-10-02 Thread Sergey via Digitalmars-d-learn
On Sunday, 2 October 2022 at 11:00:06 UTC, Daniel Donnell, Jr 
wrote:

I thought I set everything up correctly, and now:

```
Exception thrown at 0x7FF7D6E2E230 in metamath-d.exe: 
0xC096: Privileged instruction.
Unable to open natvis file 
'c:\Users\fruit\.vscode\extensions\webfreak.code-d-0.23.2\dlang-debug\dlang_cpp.natvis'.

```

So what the hell do you D developers use to code with if A) 
Visual D doesn't work - it just ate my app.obj file and can't 
find it anymore no matter if I clean or re-order the executable 
paths in settings.


B) VS Code doesn't work out-of-the-box.

Every. Single. Time I get around to using D, there's always 
something that pops up and doesn't work right.  And it never 
gets fixed.   How the heck do you expect me to debug without a 
proper debugging IDE :|


Did you try DlangIDE or Dexed?
They could be a solution



D installer

2022-10-02 Thread Imperatorn via Digitalmars-d-learn
I only have Visual Studio 2022. Will the installer be updated to 
support that or am I missing some components?


![Installer](https://i.ibb.co/sCZRFRf/installer.jpg)

Thanks!


Re: bigint and pow

2022-10-02 Thread rassoc via Digitalmars-d-learn

On 10/2/22 09:24, Fausto via Digitalmars-d-learn wrote:

Thanks a lot. I am to used to C and, more important, I didn't think to look for 
also another operator for the power function :)



Oh, and I forgot to mention that this is doing what you probably asked for 
originally:

```d
import std;
import cmath = core.stdc.math;
void main()
{
// both print 1e+72
writeln(pow(10.0, 72));
writeln(cmath.pow(10, 72));
}
```

But it's just floating-point scientific notation, not true BigInts.

Another math difference from C is that D has well-defined wrapping math for 
signed ints.


Re: bigint and pow

2022-10-02 Thread rassoc via Digitalmars-d-learn

On 10/2/22 09:24, Fausto via Digitalmars-d-learn wrote:

Thanks a lot. I am to used to C and, more important, I didn't think to look for 
also another operator for the power function :)



D does have pow and many other useful math functions [1], it's just not defined 
for BitInts. Oh, and speaking of C, you also have access to all the usual C 
math [1] functions with just an import:

```d
import std.stdio : writeln;
void main()
{
import std.math : pow;
writeln(pow(10, 3)); // pow from D

import core.stdc.math : pow;

writeln(pow(10, 3)); // pow from C

// can also make it more explicit to show where it is coming from:

import cmath = core.stdc.math;
writeln(cmath.pow(10, 3));
}
```

Have fun with D!

[1] https://dlang.org/library/std/math.html
[2] https://dlang.org/library/core/stdc/math.html



Re: What are best practices around toString?

2022-10-02 Thread Salih Dincer via Digitalmars-d-learn

On Saturday, 1 October 2022 at 17:50:54 UTC, tsbockman wrote:

but unless it is provided with a good estimate of the final
length at the beginning, it will allocate several times for
a longer string, and the final buffer will be, on average, 50% 
larger than needed.


I see, it's smart!

SDB@79




Re: Interfacing with basic C++ class

2022-10-02 Thread Riccardo M via Digitalmars-d-learn

On Friday, 30 September 2022 at 22:56:06 UTC, Ogi wrote:
On Thursday, 29 September 2022 at 12:49:06 UTC, Riccardo M 
wrote:
When interfacing to C++, disregard the keyword and look at the 
implementation instead. If all its member functions are 
non-virtual, map it to struct. Otherwise map it to class. If it 
defines at least one pure virtual member function, map it to 
abstract class. If all its member functions are either pure 
virtual or non-virtual and it contains no fields, map it to 
interface. Sounds complicated? Well, that’s because C++ is 
complicated.


Ok, in layman terms, is it correct to say that I should match the 
underlining structure of the object (e.g in terms of vtbl) so 
that C++ side and D side can work with each other correctly?


In C++, member functions defined inside its class are called 
*inline* member functions. In contrast to normal functions 
which must be defined once and only once in your program, 
inline functions must be defined in every translation unit that 
uses them. Let’s replicate your linking error in C++:


Well, I didn't know the implications of inlining member 
functions: basically when a member function is inlined, it has no 
linkage so I am pretty much done with using D in such case. While 
in C++ you can import the header and call the member function 
anyway.
The only solution would be reimplementing the offending function 
on D side.


Re: bigint and pow

2022-10-02 Thread Fausto via Digitalmars-d-learn

On Sunday, 2 October 2022 at 02:02:37 UTC, rassoc wrote:

On 10/2/22 00:04, Fausto via Digitalmars-d-learn wrote:

Hello,

I am trying to use pow with an integer argument, but I cannot 
have a bigint result, for example, ```pow(10,72)```.
Do I have to write my pow function or is there a native 
solution?


thanks,
Fausto



In contrast to certain scripting languages, there's no implicit 
promotion, you have to opt in for BigInt [1] usage in D:


```d
import std;
void main()
{
// all print the same
writeln(BigInt(10) ^^ 72);
writeln(10.BigInt ^^ 72);
writeln("10".BigInt ^^ 72);
}
```

[1] https://dlang.org/phobos/std_bigint.html#.BigInt


Thanks a lot. I am to used to C and, more important, I didn't 
think to look for also another operator for the power function :)