Re: Wouldn't the compiler be smart with this shadowing variable?

2021-12-10 Thread frame via Digitalmars-d-learn

On Friday, 10 December 2021 at 13:35:37 UTC, Matheus wrote:

I mean this is a bit weird, imagine I have different "for 
loops" and in this situation I'll need to do this:


j=0;
for(int i=0;i<10;++i){}

Otherwise:

for(i=0,j=0;i<10;++i){}

This works, but now "i" variable will be out of the "for loop" 
scope.


Matheus.


You probably want this:

```d
int j;
for({int i=0; j=0;} i<10; ++i){}
```

Beware, this syntax comes directly from hell


Re: How to loop through characters of a string in D language?

2021-12-10 Thread forkit via Digitalmars-d-learn

On Friday, 10 December 2021 at 22:35:58 UTC, Arjan wrote:


"abc;def;ghi".tr(";", "", "d" );



I don't think we have enough ways of doing the same thing yet...

so here's one more..

"abc;def;ghi".substitute(";", "");



Re: How to loop through characters of a string in D language?

2021-12-10 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Friday, 10 December 2021 at 18:47:53 UTC, Stanislav Blinov 
wrote:
Oooh, finally someone suggested to preallocate storage for all 
these reinventions of the wheel :D


```
import std.stdio;

char[] dontdothis(string s, int i=0, int skip=0){
if (s.length == i) return new char[](i - skip);
if (s[i] == ';') return dontdothis(s, i+1, skip+1);
auto r = dontdothis(s, i+1, skip);
r[i-skip] = s[i];
return r;
}

int main() {
string s = "abc;def;ab";
string s_new = cast(string)dontdothis(s);
writeln(s_new);
return 0;
}
```



Re: How to loop through characters of a string in D language?

2021-12-10 Thread Arjan via Digitalmars-d-learn

On Friday, 10 December 2021 at 06:24:27 UTC, Rumbu wrote:

On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote:

Let's say I want to skip characters and build a new string.
The character I want to skip: `;`

Expected result:
```
abcdefab
```


Since it seems there is a contest here:

```d
"abc;def;ghi".split(';').join();
```

:)

```d
"abc;def;ghi".tr(";", "", "d" );
```



Re: How to loop through characters of a string in D language?

2021-12-10 Thread forkit via Digitalmars-d-learn

On Friday, 10 December 2021 at 12:15:18 UTC, Rumbu wrote:


I thought it's a beauty contest.



Well, if it's a beauty contest, then i got a beauty..

char[("abc;def;ab".length - count("abc;def;ab", ";"))] b = 
"abc;def;ab".replace(";", "");




Re: Wouldn't the compiler be smart with this shadowing variable?

2021-12-10 Thread Siarhei Siamashka via Digitalmars-d-learn

On Friday, 10 December 2021 at 13:35:37 UTC, Matheus wrote:

Hi,

Wouldn't the compiler be smart with this shadowing variable, 
example:


void main(){
int j;
for(int i=0,j=0;i<10;++i){}
return;
}

onlineapp.d(3): Error: variable `j` is shadowing variable 
`onlineapp.main.j`


So in the "for loop" shouldn't "i" be declared and "j" just be 
assigned to 0?


Intuitively, there are two possible interpretations here for 
inexperienced humans (and you seem to be favoring the latter):
   1. declare a new "j" variable to be visible only inside of the 
loop.

   2. reuse the existing "j" variable.

But only one of them is a correct description of what actually 
happens when the compiler processes this code. So it's a good 
thing that the compiler is smart enough to reject ambiguous code 
and prevent humans from making mistakes.


Re: How to loop through characters of a string in D language?

2021-12-10 Thread Luís Ferreira via Digitalmars-d-learn
Yes it will. You can use lazy templates instead, like splitter and joiner, 
which splits and joins lazily, respectively. LDC can optimize those templates 
fairly well and avoid too much lazy calls and pretty much constructs the logic 
equivalent to for loop.

On 10 December 2021 11:06:21 WET, IGotD- via Digitalmars-d-learn 
 wrote:
>On Friday, 10 December 2021 at 06:24:27 UTC, Rumbu wrote:
>
>> 
>> Since it seems there is a contest here:
>> 
>> ```d
>> "abc;def;ghi".split(';').join();
>> ```
>> 
>> :)
>
>Would that become two for loops or not?


Re: How to loop through characters of a string in D language?

2021-12-10 Thread Rumbu via Digitalmars-d-learn
On Friday, 10 December 2021 at 18:47:53 UTC, Stanislav Blinov 
wrote:


Be interesting to see if this thread does evolve into a SIMD



http://lemire.me/blog/2017/01/20/how-quickly-can-you-remove-spaces-from-a-string/


Re: How to loop through characters of a string in D language?

2021-12-10 Thread Stanislav Blinov via Digitalmars-d-learn

On Friday, 10 December 2021 at 13:22:58 UTC, Matheus wrote:


My C way of thinking while using D:

import std;

string stripsemicolons(string input){
char[] s = input.dup;
int j=0;
for(int i=0;i


Oooh, finally someone suggested to preallocate storage for all 
these reinventions of the wheel :D


I would suggest instead of the final idup checking the length and 
only duplicating if certain waste threshold is broken, otherwise 
just doing 
https://dlang.org/phobos/std_exception.html#assumeUnique (or a 
cast to string). The result is unique either way.


Threshold could be relative for short strings and absolute for 
long ones. Makes little sense reallocating if you only waste a 
couple bytes, but makes perfect sense if you've just removed 
pages and pages of semicolons ;)


Be interesting to see if this thread does evolve into a SIMD 
search...


Re: How to use inheritance when interfacing with C++ classes?

2021-12-10 Thread rempas via Digitalmars-d-learn

On Friday, 10 December 2021 at 16:42:33 UTC, Tim wrote:


All virtual methods have to match exactly including order. If a 
virtual method is missing or added, then calling this or a 
later virtual method could call the wrong method or generate a 
segmentation fault. Non-virtual methods have to be marked final 
in D, but can also be removed.


Great info! I'll have everything in mind! Thanks a lot and have 
an amazing day!!!


Re: How to use inheritance when interfacing with C++ classes?

2021-12-10 Thread Tim via Digitalmars-d-learn

On Friday, 10 December 2021 at 12:46:07 UTC, rempas wrote:

On Thursday, 9 December 2021 at 21:35:14 UTC, Tim wrote:
Methods, which are not virtual in C++, also have to be marked 
final in D, because C++ and D use a different default.


Is this a must or just good practices?


All virtual methods have to match exactly including order. If a 
virtual method is missing or added, then calling this or a later 
virtual method could call the wrong method or generate a 
segmentation fault. Non-virtual methods have to be marked final 
in D, but can also be removed.


Re: dpq2 documentation broken

2021-12-10 Thread Adam D Ruppe via Digitalmars-d-learn

On Friday, 10 December 2021 at 14:13:53 UTC, kdevel wrote:
On https://code.dlang.org/packages/dpq2 I clicked at 
https://dpq2.dpldocs.info/v1.0.23/ and got:



try it now

the server ran out of disk space yesterday and threw some things 
off so i have to hit retry on a few of them (or like the error 
message says, it will auto-retry if you just give it more time)


dpq2 documentation broken

2021-12-10 Thread kdevel via Digitalmars-d-learn
On https://code.dlang.org/packages/dpq2 I clicked at 
https://dpq2.dpldocs.info/v1.0.23/ and got:


```
Downloading package info...
Downloading source code for version v1.0.23 from 
https://github.com/denizzzka/dpq2/archive/v1.0.23.zip...

Unzipping D files...
Generating documentation... this may take a few minutes...
$ './doc2' '--copy-standard-files=false' '--header-title' 'dpq2 
v1.0.23 (2021-11-22T22:39:51Z)' '--header-link' 
'Home=https://github.com/denizzzka/dpq2' '--header-link' 
'Dub=http://code.dlang.org/packages/dpq2' '--header-link' 
'Repo=https://github.com/denizzzka/dpq2' '--package-path' 
'core.*=//druntime.dpldocs.info/' '--package-path' 
'std.*=//phobos.dpldocs.info/' '--package-path' 
'arsd.*=//arsd-official.dpldocs.info/' 
'--postgresConnectionString' 'dbname=adrdox user=root' 
'--postgresVersionId' '12874' '--document-undocumented=true' 
'--arcz' '/dpldocs/dpq2/v1.0.23/generated.arcz' '-uiz' 
'/dpldocs/dpq2/v1.0.23/source/' 2>&1

connection.d(40:26)[warn]: Empty declaration
First pass processing 
/dpldocs/dpq2/v1.0.23/source/dpq2-1.0.23/example/example.d
object.Exception@doc2.d(4233): you must have a module declaration 
for this to work on it


??:? [0x6499e5]
??:? [0x655366]
??:? [0x636ddd]
doc2.d:4233 [0x58e473]
doc2.d:4328 [0x58a45e]
??:? [0x636aab]
??:? [0x6369a7]
??:? [0x6367fd]
??:? __libc_start_main [0x7fdb51f58b96]
../sysdeps/x86_64/start.S:120 [0x404889]
First pass processing 
/dpldocs/dpq2/v1.0.23/source/dpq2-1.0.23/src/dpq2/package.d
First pass processing 
/dpldocs/dpq2/v1.0.23/source/dpq2-1.0.23/src/dpq2/result.d
First pass processing 
/dpldocs/dpq2/v1.0.23/source/dpq2-1.0.23/src/dpq2/value.d
First pass processing 
/dpldocs/dpq2/v1.0.23/source/dpq2-1.0.23/src/dpq2/oids.d

[...]
```

```
The project build failed. It will try again in about 6 hours or 
you can copy/paste this link to adam (destructiona...@gmail.com) 
so he can fix the bug and/or reset it early. Or the repo is here 
https://github.com/adamdruppe/dpldocs but i don't often push so 
it might be out of date.


arsd.cgi.ConnectionException@/home/me/program/lib/arsd/cgi.d(4344): Broken pipe

??:? void arsd.cgi.sendAll(std.socket.Socket, const(void)[], 
immutable(char)[], ulong) [0x6121fa]
??:? void arsd.cgi.doThreadScgiConnection!(arsd.cgi.Cgi, 
dl.app(arsd.cgi.Cgi), 
500L).doThreadScgiConnection(std.socket.Socket).writeScgi(const(ubyte)[]) [0x61d3a3]

??:? void arsd.cgi.Cgi.write(const(void)[], bool, bool) [0x60c94d]
??:? void dl.app(arsd.cgi.Cgi).__lambda24(immutable(char)[]) 
[0x567ee1]
??:? void dl.rebuild(void delegate(immutable(char)[]), 
immutable(char)[], immutable(char)[]) [0x56a083]

??:? void dl.app(arsd.cgi.Cgi) [0x567a70]
??:? void arsd.cgi.doThreadScgiConnection!(arsd.cgi.Cgi, 
dl.app(arsd.cgi.Cgi), 
500L).doThreadScgiConnection(std.socket.Socket) [0x61d144]
??:? void 
arsd.cgi.ListeningConnectionManager.dg_handler(std.socket.Socket) 
[0x611c06]

??:? void arsd.cgi.ConnectionThread.run() [0x612408]
??:? void core.thread.context.Callable.opCall() [0x6599fc]
??:? thread_entryPoint [0x6594da]
??:? [0x7fe50eca86da]
??:? clone [0x7fe50e00fa3e]
```




Wouldn't the compiler be smart with this shadowing variable?

2021-12-10 Thread Matheus via Digitalmars-d-learn

Hi,

Wouldn't the compiler be smart with this shadowing variable, 
example:


void main(){
int j;
for(int i=0,j=0;i<10;++i){}
return;
}

onlineapp.d(3): Error: variable `j` is shadowing variable 
`onlineapp.main.j`


So in the "for loop" shouldn't "i" be declared and "j" just be 
assigned to 0?


I mean this is a bit weird, imagine I have different "for loops" 
and in this situation I'll need to do this:


j=0;
for(int i=0;i<10;++i){}

Otherwise:

for(i=0,j=0;i<10;++i){}

This works, but now "i" variable will be out of the "for loop" 
scope.


Matheus.


Re: How to loop through characters of a string in D language?

2021-12-10 Thread Matheus via Digitalmars-d-learn

On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote:

...
The character I want to skip: `;`


My C way of thinking while using D:

import std;

string stripsemicolons(string input){
char[] s = input.dup;
int j=0;
for(int i=0;i

Re: How to use inheritance when interfacing with C++ classes?

2021-12-10 Thread rempas via Digitalmars-d-learn

On Thursday, 9 December 2021 at 21:35:14 UTC, Tim wrote:


The referenced methods like Fl_Widget::_clear_fullscreen are 
implemented directly in the header, so the D code also needs 
the implementation, because it is not included in the compiled 
library.


What is funny about that is that I looked an the official class 
reference and copy pasted the code from here but I also looked at 
the header files and saw what you said but for some reason it 
completely slipped from my head...


Methods, which are not virtual in C++, also have to be marked 
final in D, because C++ and D use a different default.


Is this a must or just good practices?




Re: How to loop through characters of a string in D language?

2021-12-10 Thread Rumbu via Digitalmars-d-learn

On Friday, 10 December 2021 at 11:06:21 UTC, IGotD- wrote:

On Friday, 10 December 2021 at 06:24:27 UTC, Rumbu wrote:



Since it seems there is a contest here:

```d
"abc;def;ghi".split(';').join();
```

:)


Would that become two for loops or not?


I thought it's a beauty contest.

```d
string stripsemicolons(string s)
{
  string result;
  // prevent reallocations
  result.length = s.length;
  result.length = 0;

  //append to string only when needed
  size_t i = 0;
  while (i < s.length)
  {
size_t j = i;
while (i < s.length && s[i] != ';')
  ++i;
result ~= s[j..i];
  }
}
```



Re: How to loop through characters of a string in D language?

2021-12-10 Thread IGotD- via Digitalmars-d-learn

On Friday, 10 December 2021 at 06:24:27 UTC, Rumbu wrote:



Since it seems there is a contest here:

```d
"abc;def;ghi".split(';').join();
```

:)


Would that become two for loops or not?