Re: ImportC std support

2021-12-12 Thread Dave P. via Digitalmars-d-learn

On Sunday, 12 December 2021 at 07:40:10 UTC, ManKey wrote:

On Saturday, 11 December 2021 at 22:28:16 UTC, forkit wrote:

On Saturday, 11 December 2021 at 21:42:49 UTC, ManKey wrote:
umm... the site has search function you know ;-)


Dude, you see, it doesn't say anything about it. It says a 
little about the extension, but there is no direct answer.
This is important because C Std is very dependent on the 
compiler and its extensions, for example msvc std c does not 
work with importC. So my question is, are there any 
implementations of the standard C library that importC supports?


ImportC is not ready for general use yet. I’ve been reporting 
bugs that crop up when trying to use standard c headers, but I 
mostly use Linux and Macs so I haven’t reported any constructs it 
doesn’t understand on Windows. It’s supposed to be able to 
compile standard C11 code.


If you run into one, report it at https://issues.dlang.org and 
use the ImportC keyword!


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

2021-12-12 Thread Ola Fosheim Grøstad via Digitalmars-d-learn

On Saturday, 11 December 2021 at 19:50:55 UTC, russhy wrote:
you need to import a 8k lines of code module that itself 
imports other modules, and then the code is hard to read


I agree.

```
@safe:

auto deatheater(char stripchar)(string str) {
struct voldemort {
immutable(char)* begin, end;
bool empty(){ return begin == end; }
char front(){ return *begin; }
char back()@trusted{ return *(end-1); }
void popFront()@trusted{
while(begin != end){begin++; if (*begin != stripchar) 
break; }

}
void popBack()@trusted{
while(begin != end){end--; if (*(end-1) != stripchar) 
break; }

}
this(string s)@trusted{
begin = s.ptr;
end = s.ptr + s.length;
}
}
return voldemort(str);
}


void main() {
import std.stdio;
string str = "abc;def;ab";
foreach(c; deatheater!';'(str)) write(c);
writeln();
foreach_reverse(c; deatheater!';'(str)) write(c);
}

```




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

2021-12-12 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Sunday, 12 December 2021 at 08:58:29 UTC, Ola Fosheim Grøstad 
wrote:

this(string s)@trusted{
begin = s.ptr;
end = s.ptr + s.length;
}
}


Bug, it fails if the string ends or starts with ';'.

Fix:

```
this(string s)@trusted{
begin = s.ptr;
end = s.ptr + s.length;
while(begin!=end && *begin==stripchar) begin++;
while(begin!=end && *(end-1)==stripchar) end--;
}
```



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

2021-12-12 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
Of course, since it is easy to mess up and use ranges in the 
wrong way, you might want to add ```assert```s. That is most 
likely *helpful* to newbies that might want to use your kickass 
library function:


```
auto helpfuldeatheater(char stripchar)(string str) {
struct voldemort {
immutable(char)* begin, end;
bool empty(){ return begin == end; }
char front(){ assert(!empty); return *begin; }
char back()@trusted{ assert(!empty); return *(end-1); }
void popFront()@trusted{
assert(!empty);
		while(begin != end){begin++; if (*begin != stripchar) 
break; }

}
void popBack()@trusted{
assert(!empty);
while(begin != end){end--; if (*(end-1) != stripchar) 
break; }

}
this(string s)@trusted{
begin = s.ptr;
end = s.ptr + s.length;
while(begin!=end && *begin==stripchar) begin++;
while(begin!=end && *(end-1)==stripchar) end--;
}
}
return voldemort(str);
}
```



template ctor overload Segmentation fault

2021-12-12 Thread vit via Digitalmars-d-learn

Hello, why does this code fail to compile?

```d
struct Foo(T){
this(Rhs, this This)(scope Rhs rhs){
}

this(ref scope typeof(this) rhs){
}
}


struct Bar{
Foo!int foo;
}

void main(){
}
```

error: Segmentation fault (core dumped)


Restrict type of function parameter to a defined list of types?

2021-12-12 Thread Martin B via Digitalmars-d-learn

Hi everyone,

lets say that required is a setter method:
```
Nullable!string str(Nullable!string setter) {
return this._str = setter;
}
```

The user should be able to:
```
auto a = new A();
a.str = "abc";
```

As the setters parameter is defined to be of type 
`Nullable!string`, the compiler complains. So the user  need to 
do `a.str = nullable("abc");`


I guess a solution could be to define the setter as:
```
Nullable!string str(T)(T setter) {
return this._str = setter;
}
```
^stupid question: Is this the correct way to restrict the 
parameter type to be `string` or `Nullable!string` or is there a 
more precise way to restrict?


BTW: here is a more complete simple example of what i am talking 
about: https://run.dlang.io/is/zP4vkb


Re: Restrict type of function parameter to a defined list of types?

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

On Sunday, 12 December 2021 at 13:11:58 UTC, Martin B wrote:

Hi everyone,

lets say that required is a setter method:
```
Nullable!string str(Nullable!string setter) {
return this._str = setter;
}
```



Just add a forwarding overload:

auto str(string s) { return this.str(nullable(s)); }


Then the user can pass either Nullable or string but not other 
things.


Re: Restrict type of function parameter to a defined list of types?

2021-12-12 Thread Martin B via Digitalmars-d-learn

On Sunday, 12 December 2021 at 13:21:06 UTC, Adam D Ruppe wrote:

On Sunday, 12 December 2021 at 13:11:58 UTC, Martin B wrote:
Just add a forwarding overload:

Hi Adam,

i am wondering if there is another possibility without having to 
create overloads for each parameter type - something like this 
pseudocode:

```
Nullable!string str(T : {string, Nullable!string}) (T setter) {
return this._str = setter;
}
```


Re: Restrict type of function parameter to a defined list of types?

2021-12-12 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 12 December 2021 at 13:42:08 UTC, Martin B wrote:

On Sunday, 12 December 2021 at 13:21:06 UTC, Adam D Ruppe wrote:

On Sunday, 12 December 2021 at 13:11:58 UTC, Martin B wrote:
Just add a forwarding overload:

Hi Adam,

i am wondering if there is another possibility without having 
to create overloads for each parameter type - something like 
this pseudocode:

```
Nullable!string str(T : {string, Nullable!string}) (T setter) {
return this._str = setter;
}
```


You can use a [template constraint][1]:

```d
Nullable!string str(T)(T setter)
if (is(T == string) || is(T == Nullable!string))
{
return this._str = setter;
}
```

[1]: https://dlang.org/spec/template.html#template_constraints


Re: unit test broken [DUB bug?]

2021-12-12 Thread russhy via Digitalmars-d-learn
You are running the beta version of the compiler, and an older 
version of LDC2


I'd first try to update them to make sure you aren't missing any 
bug fixes


Re: Restrict type of function parameter to a defined list of types?

2021-12-12 Thread Martin B via Digitalmars-d-learn

On Sunday, 12 December 2021 at 14:11:48 UTC, Paul Backus wrote:

You can use a [template constraint][1]:


Hi Paul,
yes! thats it, Thanks. I am facepalming me right now because have 
been on that webpage and missed that point.


Re: template ctor overload Segmentation fault

2021-12-12 Thread Imperatorn via Digitalmars-d-learn

On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote:

Hello, why does this code fail to compile?

```d
struct Foo(T){
this(Rhs, this This)(scope Rhs rhs){
}

this(ref scope typeof(this) rhs){
}
}


struct Bar{
Foo!int foo;
}

void main(){
}
```

error: Segmentation fault (core dumped)


What are you trying to accomplish?


Re: Restrict type of function parameter to a defined list of types?

2021-12-12 Thread Imperatorn via Digitalmars-d-learn

On Sunday, 12 December 2021 at 14:26:54 UTC, Martin B wrote:

On Sunday, 12 December 2021 at 14:11:48 UTC, Paul Backus wrote:

You can use a [template constraint][1]:


Hi Paul,
yes! thats it, Thanks. I am facepalming me right now because 
have been on that webpage and missed that point.


Happens to everyone at some time 🌅


Re: template ctor overload Segmentation fault

2021-12-12 Thread vit via Digitalmars-d-learn

On Sunday, 12 December 2021 at 18:32:28 UTC, Imperatorn wrote:

On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote:

Hello, why does this code fail to compile?

```d
struct Foo(T){
this(Rhs, this This)(scope Rhs rhs){
}

this(ref scope typeof(this) rhs){
}
}


struct Bar{
Foo!int foo;
}

void main(){
}
```

error: Segmentation fault (core dumped)


What are you trying to accomplish?


Something like this:

```d
import std.traits : CopyConstness;

struct UniquePtr(T){
alias Type = T;

this(Rhs, this This)(scope Rhs rhs)
if(is(CopyConstness!(Rhs, Rhs.Type*) : CopyConstness!(This, 
This.Type*))){

//...
}

//one of copy ctors:
this(ref scope typeof(this) rhs){
//...
}

static UniquePtr make(Args...)(Args args){
return UniquePtr.init;
}
}


void main(){
const UniquePtr!(int) cui = UniquePtr!(const int).make(1);
const UniquePtr!(const int) cuci = UniquePtr!(const 
int).make(1);

UniquePtr!(const int) uci = UniquePtr!(int).make(1);
UniquePtr!(int) ui = UniquePtr!(int).make(1);

const UniquePtr!(int) xcui = UniquePtr!(immutable 
int).make(1);
const UniquePtr!(const int) xcuci = UniquePtr!(immutable 
int).make(1);


}
```

This work but UniquePtr canno't be inside struct because 
Segmentation fault.


How to pass a class by (const) reference to C++

2021-12-12 Thread Jan via Digitalmars-d-learn

In D I have an extern(C++) class:

```cpp
extern(C++) class A
{
~this();

// other stuff
}
```

An a function that takes A by const reference:

```cpp
void CppFunc(const A& arg);
```

But how do I bind this in D ?

```cpp
extern(C++) void CppFunc(A arg); // tries to pass as 'A*'
extern(C++) void CppFunc(ref const(A) arg); // tries to pass as 
'A const * const &'

```

I have solved similar problems with other classes by declaring 
them as struct in D, but that only works for classes that have no 
virtual functions. I now have a class where I do need to use a 
class on the D side, and now I have problems passing these 
objects to C++.


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

2021-12-12 Thread forkit via Digitalmars-d-learn
On Saturday, 11 December 2021 at 09:25:37 UTC, Ola Fosheim 
Grøstad wrote:


```putchar(…)``` is too slow!




On planet Mars maybe, but here on earth, my computer can do about 
4 billion ticks per second, and my entire program (using putchar) 
takes only 3084 ticks.





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

2021-12-12 Thread bauss via Digitalmars-d-learn

On Monday, 13 December 2021 at 05:46:06 UTC, forkit wrote:
On Saturday, 11 December 2021 at 09:25:37 UTC, Ola Fosheim 
Grøstad wrote:


```putchar(…)``` is too slow!




On planet Mars maybe, but here on earth, my computer can do 
about 4 billion ticks per second, and my entire program (using 
putchar) takes only 3084 ticks.


Can I borrow a couple of your ticks?


Re: How to pass a class by (const) reference to C++

2021-12-12 Thread evilrat via Digitalmars-d-learn

On Sunday, 12 December 2021 at 21:24:39 UTC, Jan wrote:

In D I have an extern(C++) class:

```cpp
extern(C++) class A
{
~this();

// other stuff
}
```

An a function that takes A by const reference:

```cpp
void CppFunc(const A& arg);
```

But how do I bind this in D ?

```cpp
extern(C++) void CppFunc(A arg); // tries to pass as 'A*'
extern(C++) void CppFunc(ref const(A) arg); // tries to pass as 
'A const * const &'

```

I have solved similar problems with other classes by declaring 
them as struct in D, but that only works for classes that have 
no virtual functions. I now have a class where I do need to use 
a class on the D side, and now I have problems passing these 
objects to C++.


You can tell compiler to mangle it as struct/class using 
extern(C++, struct).


```d
extern (C++, struct) // will use struct mangling even though it's 
a class

class SomeDClass
{
 ...
}
```