Dynamic Arrays Capacity

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

Hi,

Do I misunderstand? A dynamic array is allocated memory according 
to the `nextpow2()` algorithm(-1 lapse); strings, on the other 
hand, don't behave like this...


```d
  string str = "0123456789ABCDEF";
  char[] chr = str.dup;

  assert(str.length == 16);
  assert(str.capacity == 0);

  import std.math: thus = nextPow2; //.algebraic

  assert(chr.capacity == thus(str.length) - 1);
  assert(chr.capacity == 31);
```

Also, `.ptr` keeps the address of the most recent first element, 
right?



```d
  write("str[0]@", [0]);
  writeln(" == @", str.ptr);

  write("chr[0]@", [0]);
  writeln(" == @", chr.ptr);
```

**Print Out:** (No Errors)

str[0]@5607593901E0 == @5607593901E0
chr[0]@7F9430982000 == @7F9430982000


SDB@79


Re: Anybody have any idea on how to do shared operator overloads?

2022-06-01 Thread Tejas via Digitalmars-d-learn

On Thursday, 2 June 2022 at 01:49:32 UTC, Ruby The Roobster wrote:
On Thursday, 2 June 2022 at 01:29:39 UTC, Ruby The Roobster 
wrote:

On Thursday, 2 June 2022 at 01:00:57 UTC, Ali Çehreli wrote:

On 6/1/22 17:36, Ruby The Roobster wrote:
> A stripped down version of some code I have:

Not much experience here but I made two changes:

1) Added 'shared':

>  this(Complex!real num = Complex!real(0,0)) shared
>  {
>  this.num = num;
>  }
>  this(shared Complex!real num = cast(shared
> Complex!real)Complex!real(0,0))
>  {
>  this.num.re = num.re;
>  this.im.re = im.re;

2) Fixed apparent typos:

  this.num.im = num.im;

>  }

I can't guarantee that correct functions are called. It just 
compiles with 2.100.0. :)


Ali


Yes, those were typos.  However, when making this post, I 
forgot to mark ```this(shared Complex!real num = cast(shared 
Complex!real)Complex!real(0,0))``` as shared.  The other 
constructor is not marked as shared in my code, considering as 
shared classes have all of their members marked as shared.


I also have a bug:  __traits(compiles) only checks if the 
expressions are SEMANTICALLY correct, not if they actually 
compile, which they don't.


Interestingly, this code compiles between 2.063 and 2.066.1, if 
you were to put it into run.dlang.io, given the above changes 
(including the p1 + p2!), and set to all compilers.


I fixed the typos and added some extra constructors and also 
wrote a little more complex `opBinary` for the `shared` overload 
of `Number`. Now the thing works(AFAICT):


```d
public import std.complex;
public import std.stdio;

public interface Mtype
{
// ...
}

public class Number : Mtype
{
public:
// new code begin
this()
{
this.num = Complex!real(1,1);
}
this() shared
{
this.num = Complex!real(1,1);
}
// new code ends
this(Complex!real num = Complex!real(0,0))
{
this.num = num;
}
this(shared Complex!real num = cast(shared 
Complex!real)Complex!real(0,0)) shared

{
this.num.re = num.re;
this.num.im = num.im;
}
Number opBinary(string op)(Number rhs) //Standard opBinary
{
mixin("return new Number(this.num " ~ op ~ " 
rhs.num);");

}
shared(Number) opBinary(string op)(shared Number rhs) 
shared

//changed this code a little as well
{
mixin(q{return new shared 
Number(Complex!real(this.num.re} ~ op ~ q{rhs.num.re, 
this.num.im} ~ op ~ q{rhs.num.im));}); //Placeholder untill I can 
get the code to work

}
package:
Complex!real num;
}

bool isMtype(T)()
{
bool ret = true;
// ...
shared T p = new shared T();
shared T p2 = new shared T();
ret &= __traits(compiles, T, p + p2);
return ret;
}

static assert(isMtype!Number); //This fails. Not anymore :D

void main()
{
shared num1 = new shared Number();
shared num2 = new shared Number();
auto num3 = num1 + num2;
writeln("real: ", num3.num.re, "\nimaginary: ",num3.num.im);
}
```


Graphing a D function : possible?

2022-06-01 Thread z via Digitalmars-d-learn
Is there a quick way of obtaining the graph of D functions like 
these?

```d
T f(T) if (isScalarType!T){}
```
or
```D
T[2] f(T, T)if (isScalarType!T){}
```
I know that there are graphing calculators already, but these 
don't support low level black magic like int <-> float 
conversions and i'm lost because there is no way to know if the 
code i write is correct without a graph or trial and error, hence 
the question.


Many thanks


Re: Anybody have any idea on how to do shared operator overloads?

2022-06-01 Thread Ruby The Roobster via Digitalmars-d-learn

On Thursday, 2 June 2022 at 01:29:39 UTC, Ruby The Roobster wrote:

On Thursday, 2 June 2022 at 01:00:57 UTC, Ali Çehreli wrote:

On 6/1/22 17:36, Ruby The Roobster wrote:
> A stripped down version of some code I have:

Not much experience here but I made two changes:

1) Added 'shared':

>  this(Complex!real num = Complex!real(0,0)) shared
>  {
>  this.num = num;
>  }
>  this(shared Complex!real num = cast(shared
> Complex!real)Complex!real(0,0))
>  {
>  this.num.re = num.re;
>  this.im.re = im.re;

2) Fixed apparent typos:

  this.num.im = num.im;

>  }

I can't guarantee that correct functions are called. It just 
compiles with 2.100.0. :)


Ali


Yes, those were typos.  However, when making this post, I forgot 
to mark ```this(shared Complex!real num = cast(shared 
Complex!real)Complex!real(0,0))``` as shared.  The other 
constructor is not marked as shared in my code, considering as 
shared classes have all of their members marked as shared.


I also have a bug:  __traits(compiles) only checks if the 
expressions are SEMANTICALLY correct, not if they actually 
compile, which they don't.


Interestingly, this code compiles between 2.063 and 2.066.1, if 
you were to put it into run.dlang.io, given the above changes 
(including the p1 + p2!), and set to all compilers.


Re: Anybody have any idea on how to do shared operator overloads?

2022-06-01 Thread Ruby The Roobster via Digitalmars-d-learn

On Thursday, 2 June 2022 at 01:00:57 UTC, Ali Çehreli wrote:

On 6/1/22 17:36, Ruby The Roobster wrote:
> A stripped down version of some code I have:

Not much experience here but I made two changes:

1) Added 'shared':

>  this(Complex!real num = Complex!real(0,0)) shared
>  {
>  this.num = num;
>  }
>  this(shared Complex!real num = cast(shared
> Complex!real)Complex!real(0,0))
>  {
>  this.num.re = num.re;
>  this.im.re = im.re;

2) Fixed apparent typos:

  this.num.im = num.im;

>  }

I can't guarantee that correct functions are called. It just 
compiles with 2.100.0. :)


Ali


Yes, those were typos.  However, when making this post, I forgot 
to mark ```this(shared Complex!real num = cast(shared 
Complex!real)Complex!real(0,0))``` as shared.  The other 
constructor is not marked as shared in my code, considering as 
shared classes have all of their members marked as shared.


Re: Anybody have any idea on how to do shared operator overloads?

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

On 6/1/22 17:36, Ruby The Roobster wrote:
> A stripped down version of some code I have:

Not much experience here but I made two changes:

1) Added 'shared':

>  this(Complex!real num = Complex!real(0,0)) shared
>  {
>  this.num = num;
>  }
>  this(shared Complex!real num = cast(shared
> Complex!real)Complex!real(0,0))
>  {
>  this.num.re = num.re;
>  this.im.re = im.re;

2) Fixed apparent typos:

  this.num.im = num.im;

>  }

I can't guarantee that correct functions are called. It just compiles 
with 2.100.0. :)


Ali



Anybody have any idea on how to do shared operator overloads?

2022-06-01 Thread Ruby The Roobster via Digitalmars-d-learn

A stripped down version of some code I have:

```d
public import std.complex;

public interface Mtype
{
// ...
}

public class Number : Mtype
{
public:
this(Complex!real num = Complex!real(0,0))
{
this.num = num;
}
this(shared Complex!real num = cast(shared 
Complex!real)Complex!real(0,0))

{
this.num.re = num.re;
this.im.re = im.re;
}
Number opBinary(string op)(Number rhs) //Standard opBinary
{
mixin("return new Number(this.num " ~ op ~ " 
rhs.num);");

}
shared(Number) opBinary(string op)(shared Number rhs) 
shared

{
return new shared Number(); //Placeholder untill I 
can get the code to work

}
package:
Complex!real num;
}

bool isMtype(T)()
{
bool ret = true;
// ...
shared T p = new shared T();
shared T p2 = new shared T();
ret &= __traits(compiles, T, p + p2);
return ret;
}

static assert(isMtype!Number); //This fails.
```

Upon adding the line shared T c = p + p2 to isMtype,  I get the 
following error:




source\dutils\math\core.d(138,22): Error: function call through 
null class reference `null`
source\dutils\math\core.d(142,15):called from here: 
`isMtype()`
source\dutils\math\core.d(142,1):while evaluating: 
`static assert(cast(int)isMtype() == 1)`


Anybody know how to get a working shared operator overload and 
fix this mess?


Re: Basic SQLite Application

2022-06-01 Thread Adam D Ruppe via Digitalmars-d-learn

On Wednesday, 1 June 2022 at 15:40:43 UTC, harakim wrote:
It's been a long time since I did any C development, and I have 
never done any on windows, but I thought I could statically 
link to the .lib at compile time and then I wouldn't need a dll.


You sometimes can, it depends on how the library is built. If it 
was built as a dll, you need to use it that way unless you 
recompile the library itself.


Re: Basic SQLite Application

2022-06-01 Thread harakim via Digitalmars-d-learn

On Wednesday, 1 June 2022 at 15:58:01 UTC, Jesse Phillips wrote:

On Wednesday, 1 June 2022 at 15:40:43 UTC, harakim wrote:
It's been a long time since I did any C development, and I 
have never done any on windows, but I thought I could 
statically link to the .lib at compile time and then I 
wouldn't need a dll. I'm fine with using a dll, but I don't 
know how to get the corresponding .bin. I'm guessing there is 
just a c header file. Is this a case where I would need to 
make bindings?



In this case this lib is the dynamic bindings to the dll.


Thanks for that reply. That makes sense.


Re: Basic SQLite Application

2022-06-01 Thread Jesse Phillips via Digitalmars-d-learn

On Wednesday, 1 June 2022 at 15:40:43 UTC, harakim wrote:
It's been a long time since I did any C development, and I have 
never done any on windows, but I thought I could statically 
link to the .lib at compile time and then I wouldn't need a 
dll. I'm fine with using a dll, but I don't know how to get the 
corresponding .bin. I'm guessing there is just a c header file. 
Is this a case where I would need to make bindings?



In this case this lib is the dynamic bindings to the dll.


Re: Basic SQLite Application

2022-06-01 Thread harakim via Digitalmars-d-learn

On Wednesday, 1 June 2022 at 10:57:11 UTC, Adam D Ruppe wrote:

BTW:

"copyFiles":["lib/sqlite3.lib"]


You don't need that, the .lib is only used while building. You 
might need to copyFiles the .dll though.


It's been a long time since I did any C development, and I have 
never done any on windows, but I thought I could statically link 
to the .lib at compile time and then I wouldn't need a dll. I'm 
fine with using a dll, but I don't know how to get the 
corresponding .bin. I'm guessing there is just a c header file. 
Is this a case where I would need to make bindings?


As to the issue at hand, I found that bin linked from another 
dlang thread where someone was trying to get sqlite working. It 
linked to this repository: 
https://github.com/buggins/ddbc/tree/master/libs/win64


So when you said it might be the wrong dll, what I did is I 
grabbed the dll from there also and it worked. 


Thanks for your help once again.


Re: freebsd dub linker error

2022-06-01 Thread Kagamin via Digitalmars-d-learn

Try to run clang with -v option and compare with gcc.


Re: freebsd dub linker error

2022-06-01 Thread Alain De Vos via Digitalmars-d-learn

The detailed error is :
```
/usr/bin/clang test.o -o test -L/usr/local/lib -lphobos2-ldc 
-ldruntime-ldc -Wl,--gc-sections -lexecinfo -lpthread -lm -m64

ld: error: undefined hidden symbol: __start___minfo

referenced by test.d
  test.o:(ldc.register_dso)


```


Re: freebsd dub linker error

2022-06-01 Thread Alain De Vos via Digitalmars-d-learn

Performed additional tests.
Compiling helloworld.d
```
export CC=gcc11 ; ldc2 helloworld.d
```
works fine.

Compiling helloworld.d
```
export CC=clang ; ldc2 helloworld.d
```
returns:
```
d: error: undefined hidden symbol: __start___minfo

referenced by test.d
  test.o:(ldc.register_dso)


ld: error: undefined hidden symbol: __stop___minfo

referenced by test.d
  test.o:(ldc.register_dso)
clang: error: linker command failed with exit code 1 (use -v to 
see invocation)

Error: /usr/bin/clang failed with status: 1
```

So I have a workaround by fixing it to gcc


Re: Basic SQLite Application

2022-06-01 Thread Adam D Ruppe via Digitalmars-d-learn

On Wednesday, 1 June 2022 at 03:46:38 UTC, harakim wrote:
I started trying to get it to compile in another directory 
structure but since I've switched to dub


It should work the way you have it, just with dub you can also 
the dub version instead of copying the files: 
https://code.dlang.org/packages/arsd-official%3Asqlite


both are supposed to work.

anyway

it compiles and runs and returns some large negative number as 
an error without printing what's in the writeln.


What is the number? My guess is you might have gotten the wrong 
sqlite3.dll (it should come from the same source as the .lib file 
you used) or it is in the wrong place.


I won't rule out that my lib file is the wrong file as I don't 
know how to tell or find the right one.


That's possible too but it would normally fail to link entirely 
if this was it. My money is on the dll, especially since the 
main() doesn't even try to open the database, it must be a 
loading issue. Where did you get the .lib file anyway?


BTW:

"copyFiles":["lib/sqlite3.lib"]


You don't need that, the .lib is only used while building. You 
might need to copyFiles the .dll though.