Re: Convering strings containing number

2022-06-14 Thread Mike Parker via Digitalmars-d-learn

On Wednesday, 15 June 2022 at 04:26:44 UTC, Salih Dincer wrote:

Hi,

I've been interested in conversion possibilities for a while.  
I tried to convert a string containing numbers but with no 
success in single digits.  The only solution I found is to 
subtract 48 from the result:


```d
import std;
void main()
{
    string str = "abc123";
    str[3..$].to!int.writeln; // 123
    auto bar = str[3].to!int;
    assert(bar == 49); // 49 is value for ASCII.
    writeln(bar - 48); // 1
}
```


By indexing `str`, you're getting a `char`. So `to` is operating 
on that rather than on a string. Slicing will give you what you 
want, since then you'd have a `"1"` rather than a `'1'`:


```d
str[3..4].to!int;
```


Convering strings containing number

2022-06-14 Thread Salih Dincer via Digitalmars-d-learn

Hi,

I've been interested in conversion possibilities for a while.  I 
tried to convert a string containing numbers but with no success 
in single digits.  The only solution I found is to subtract 48 
from the result:


```d
import std;
void main()
{
    string str = "abc123";
    str[3..$].to!int.writeln; // 123
    auto bar = str[3].to!int;
    assert(bar == 49); // 49 is value for ASCII.
    writeln(bar - 48); // 1
}
```


Re: Comparing Exceptions and Errors

2022-06-14 Thread Steven Schveighoffer via Digitalmars-d-learn
I don't see what you see wrong with the code I wrote. It's 
straightforward, obvious, and does the job I need it to do, in a way 
that's not prone to future mistakes.


I explained why, but you don't agree with the explanation. That's OK, we 
don't all have to write the same exact systems. D is a language that can 
satisfy many needs and philosophies! If you find it doesn't do it quite 
the way you want, and C++ does, C++ is also a fine language to use.


-Steve


Re: Bug in dmd?

2022-06-14 Thread Andrey Zherikov via Digitalmars-d-learn

On Tuesday, 14 June 2022 at 21:44:48 UTC, Dukc wrote:
No idea. The functions seems indeed to be exactly the same, so 
I assume this is a DMD bug. It cannot be a bug in 
`std.sumtype`, since that would trigger in both of the 
templates.


This definitely triggers some bug in DMD because this `private 
void printHelp ...` function is not really `private` as it's 
called from [another 
module](https://github.com/andrey-zherikov/argparse/blob/bug/source/argparse/internal.d#L786) and DMD doesn't catch this.


Re: Comparing Exceptions and Errors

2022-06-14 Thread kdevel via Digitalmars-d-learn
On Monday, 13 June 2022 at 13:15:42 UTC, Steven Schveighoffer 
wrote:


Possibly, but I don't close the handle. It goes back to a pool 
to get reused.


Um. I need a (fresh) connection per CGI process. I wonder if 
nowadays the TLS startup between the browser and the webserver 
isn't at least one or two magnitudes more expensive than the 
(unencrypted) connection to the domain socket of the DB engine. 
May pooling DB connections instead of closing them be optimizing 
on the wrong end?



Why not have a Transaction class (struct)?


A transaction class/struct cannot hook normal return and 
throwing differently (the destructor has no way of knowing 
whether an exception is in flight).


Regarding C++: “[...] In a proper robust design, commits should 
always be explicit and destructors only rollback uncommitted 
transactions. [...]


There is now enough introspection in the language to actually 
implement implicit commit in a non exceptional path, but I think 
it would be a mistake."


[1] https://news.ycombinator.com/item?id=24648406


Re: Bug in dmd?

2022-06-14 Thread Dukc via Digitalmars-d-learn

On Tuesday, 14 June 2022 at 13:39:12 UTC, Andrey Zherikov wrote:
I have [pretty simple code in my 
library](https://github.com/andrey-zherikov/argparse/blob/bug/source/argparse/help.d#L27-L47):

```d
alias CC = SumType!(AA,BB);
struct AA {}
struct BB
{
CC[] c;
}

private void ppp(T, Output)(auto ref Output output, in 
CommandArguments!T cmd, in Config config)

{
auto cc = CC(AA());  // (1)
}
private void printHelp(T, Output)(auto ref Output output, in 
CommandArguments!T cmd, in Config config)

{
auto cc = CC(AA());  // (2)
}

void printHelp(T, Output)(auto ref Output output, in Config 
config)

{
ppp(output, CommandArguments!T(config), config);
printHelp(output, CommandArguments!T(config), config);
}
```

[Line (2) 
produces](https://github.com/andrey-zherikov/argparse/runs/6880350900?check_suite_focus=true#step:5:12) `undefined reference to '_D3std7sumtype__T7SumTypeTS8argparse4help2AATSQtQm2BBZQBl6__dtorMFNaNbNiNfZv'` (it demangles to `pure nothrow @nogc @safe void std.sumtype.SumType!(argparse.help.AA, argparse.help.BB).SumType.__dtor()`)


If I comment line (2) then everything is good (no link errors). 
Note that there is the same code at (1) which doesn't produce 
any error. Any idea what's going on?


No idea. The functions seems indeed to be exactly the same, so I 
assume this is a DMD bug. It cannot be a bug in `std.sumtype`, 
since that would trigger in both of the templates.


BTW, thanks for taking the effort to write your question this 
well (demagling, link to your library and all).


Re: Bug?

2022-06-14 Thread Steven Schveighoffer via Digitalmars-d-learn

On 6/14/22 3:35 PM, JG wrote:

Hi,

Is this a bug?
```d
import std;

template test(alias f) {

     auto test(I)(I i) { return f(i); }
}

void main()
{
     alias t = test!(x=>x+1);
     1.t.writeln; //<--Doesn't compile
     1.test!(x=>x+1).writeln;
     t(1).writeln;
}
```


Not a bug. Local symbols can't be used as UFCS functions.

-Steve


Bug?

2022-06-14 Thread JG via Digitalmars-d-learn

Hi,

Is this a bug?
```d
import std;

template test(alias f) {

auto test(I)(I i) { return f(i); }
}

void main()
{
alias t = test!(x=>x+1);
1.t.writeln; //<--Doesn't compile
1.test!(x=>x+1).writeln;
t(1).writeln;
}
```


Re: Closures over temporary variables

2022-06-14 Thread Ali Çehreli via Digitalmars-d-learn

On 6/14/22 02:04, bauss wrote:

> You have to do it like this:
>
> ```
> dgs ~= ( (n) => () { writeln(n); })(i);
> ```

The same thing with a named function as well as with iota():

import std.range;
import std.algorithm;
import std.stdio;

void main() {
  void delegate()[] dgs;

  auto makeDg(int i) {
return () => writeln(i);
  }

  foreach (immutable i; 0 .. 3)
  {
dgs ~= makeDg(i);
  }

  iota(3).each!(i => dgs ~= () => writeln(i));

  foreach (dg; dgs)
  {
dg();
  }
}

Ali



Re: ImportC: unresolved external symbol

2022-06-14 Thread ryuukk_ via Digitalmars-d-learn

On Tuesday, 14 June 2022 at 14:38:17 UTC, Mike Parker wrote:

On Tuesday, 14 June 2022 at 14:32:50 UTC, ryuukk_ wrote:



```
nk.obj : error LNK2019: unresolved external symbol test 
referenced in function _Dmain

```

Am i missing something important? (that is a dub project, 
created with: dub init)


DMD: v2.100.0-dirty


This works from the command line:

```
dmd -m64 app.d nk.c
```

It's possible dub isn't passing the C file to the compiler.



That was the issue!

Adding the file manually in the dub.json solved it!

```
"sourceFiles": [ "source/nk.c" ]
```

That should be easy to fix, i'll fill a PR

Thanks




Re: ImportC: unresolved external symbol

2022-06-14 Thread Mike Parker via Digitalmars-d-learn

On Tuesday, 14 June 2022 at 14:32:50 UTC, ryuukk_ wrote:



```
nk.obj : error LNK2019: unresolved external symbol test 
referenced in function _Dmain

```

Am i missing something important? (that is a dub project, 
created with: dub init)


DMD: v2.100.0-dirty


This works from the command line:

```
dmd -m64 app.d nk.c
```

It's possible dub isn't passing the C file to the compiler. 
Assuming everything's configured properly (e.g., both files are 
in the same source directory, or the paths are properly set if 
they aren't), you should do a verbose build and see what the 
compiler command line looks like.


ImportC: unresolved external symbol

2022-06-14 Thread ryuukk_ via Digitalmars-d-learn

Hello

Tried to give ImportC a try to finally get rid of gluecode in one 
of my project, but this simple test gives me a compile error:



nk.c
```
int test(void)
{
return 1;
}
```

app.d
```
import std.stdio;
import nk = nk;

void main()
{
if (nk.test() != 0)
{
writeln("test");
}
}

```

```
nk.obj : error LNK2019: unresolved external symbol test 
referenced in function _Dmain

```

Am i missing something important? (that is a dub project, created 
with: dub init)


DMD: v2.100.0-dirty



Bug in dmd?

2022-06-14 Thread Andrey Zherikov via Digitalmars-d-learn
I have [pretty simple code in my 
library](https://github.com/andrey-zherikov/argparse/blob/bug/source/argparse/help.d#L27-L47):

```d
alias CC = SumType!(AA,BB);
struct AA {}
struct BB
{
CC[] c;
}

private void ppp(T, Output)(auto ref Output output, in 
CommandArguments!T cmd, in Config config)

{
auto cc = CC(AA());  // (1)
}
private void printHelp(T, Output)(auto ref Output output, in 
CommandArguments!T cmd, in Config config)

{
auto cc = CC(AA());  // (2)
}

void printHelp(T, Output)(auto ref Output output, in Config 
config)

{
ppp(output, CommandArguments!T(config), config);
printHelp(output, CommandArguments!T(config), config);
}
```

[Line (2) 
produces](https://github.com/andrey-zherikov/argparse/runs/6880350900?check_suite_focus=true#step:5:12) `undefined reference to '_D3std7sumtype__T7SumTypeTS8argparse4help2AATSQtQm2BBZQBl6__dtorMFNaNbNiNfZv'` (it demangles to `pure nothrow @nogc @safe void std.sumtype.SumType!(argparse.help.AA, argparse.help.BB).SumType.__dtor()`)


If I comment line (2) then everything is good (no link errors). 
Note that there is the same code at (1) which doesn't produce any 
error. Any idea what's going on?


Re: Closures over temporary variables

2022-06-14 Thread bauss via Digitalmars-d-learn

On Tuesday, 14 June 2022 at 08:26:53 UTC, Anonymouse wrote:

What is the correct way of making this output `0 1 2`?

```d
void delegate()[] dgs;

foreach (immutable i; 0..3)
{
dgs ~= () => writeln(i);
}

foreach (dg; dgs)
{
dg();  // outputs: `2 2 2`
}
```


You have to do it like this:

```
dgs ~= ( (n) => () { writeln(n); })(i);
```

Because D hasn't fixed their million dollar mistake after so many 
years:


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

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





how to set REST path for gcloud api in vibe.d?

2022-06-14 Thread MichaelBi via Digitalmars-d-learn
I need to use gcloud NLP api which has the url and endpoint like 
this 
"https://language.googleapis.com/v1beta2/documents:analyzeSentiment;. The ":" between documents and analyzeSentiment make a lot trouble for me. Just don't know how to setup the @path in RestInterface for such endpoint. I tried many, including `/documents:analyzeSentiment` but all failed, cannot go to the right path...


any idea? thank in advance.


Closures over temporary variables

2022-06-14 Thread Anonymouse via Digitalmars-d-learn

What is the correct way of making this output `0 1 2`?

```d
void delegate()[] dgs;

foreach (immutable i; 0..3)
{
dgs ~= () => writeln(i);
}

foreach (dg; dgs)
{
dg();  // outputs: `2 2 2`
}
```