Re: auto ref function parameter causes that non copyable struct is copied?

2021-11-08 Thread tchaloupka via Digitalmars-d-learn

On Tuesday, 9 November 2021 at 02:43:55 UTC, jfondren wrote:

On Tuesday, 9 November 2021 at 02:41:18 UTC, jfondren wrote:
The expectation is probably that `f.move` set `f` to 
`Foo.init`, but the docs say:


Posted too fast. Foo qualifies with its @disable:

```d
struct A { int x; }
struct B { int x; @disable this(this); }

unittest {
import core.lifetime : move;
auto a = A(5);
auto b = B(5);
a.move;
b.move;
assert(a == A(5));
assert(b == B(0));
}
```


Yes it should qualify so it should be cleaned out when moved.
When I change:

```D
auto ref unwrap(EX)(auto ref EX res) {
printf("unwrap()\n");
return res.get();
}
```

I get:

```
~this(0)
~this(0)
~this(0)
unwrap()
~this(42)
~this(0)
~this(42)
```

So the destructor isn't called from `gen()` -> there is 
[NRVO](https://dlang.org/glossary.html#nrvo) in play.


Also `pragma(msg, __traits(isRef, res));` prints false in unwrap 
in this case (which is expected), but how it gets there as a 
value when it's not moved and instead copied even though it has 
disabled copy constructor?

`isCopyable!(Value!Foo)` returns false as expected too.


Re: auto ref function parameter causes that non copyable struct is copied?

2021-11-08 Thread jfondren via Digitalmars-d-learn

On Tuesday, 9 November 2021 at 02:41:18 UTC, jfondren wrote:
The expectation is probably that `f.move` set `f` to 
`Foo.init`, but the docs say:


Posted too fast. Foo qualifies with its @disable:

```d
struct A { int x; }
struct B { int x; @disable this(this); }

unittest {
import core.lifetime : move;
auto a = A(5);
auto b = B(5);
a.move;
b.move;
assert(a == A(5));
assert(b == B(0));
}
```


Re: auto ref function parameter causes that non copyable struct is copied?

2021-11-08 Thread jfondren via Digitalmars-d-learn
On Tuesday, 9 November 2021 at 02:19:28 UTC, Stanislav Blinov 
wrote:

On Monday, 8 November 2021 at 23:26:39 UTC, tchaloupka wrote:


```
auto gen() {
Foo f;   // <--- this one
f.n = 42;
return value(f.move());
}

void main() {
Foo f;
f = gen().unwrap.move;
}
```
~this(0)
~this(0)
~this(0)
~this(42) <- this is a copy (that shouldn't exist) being 
destroyed

~this(0)
~this(42)



Is it a copy? I think the first destructor call is one of `f` 
in `gen` (marked by the comment above)


The expectation is probably that `f.move` set `f` to `Foo.init`, 
but the docs say:


"If T is a struct with a destructor or postblit defined, source 
is reset to its .init value after it is moved into target, 
otherwise it is left unchanged."


```d
struct A { int x; }
struct B { int x; this(this) { } }

unittest {
import core.lifetime : move;
auto a = A(5);
auto b = B(5);
a.move;
b.move;
assert(a == A(5));
assert(b == B(0));
}
```


Re: auto ref function parameter causes that non copyable struct is copied?

2021-11-08 Thread Stanislav Blinov via Digitalmars-d-learn

On Monday, 8 November 2021 at 23:26:39 UTC, tchaloupka wrote:


```
auto gen() {
Foo f;   // <--- this one
f.n = 42;
return value(f.move());
}

void main() {
Foo f;
f = gen().unwrap.move;
}
```
~this(0)
~this(0)
~this(0)
~this(42) <- this is a copy (that shouldn't exist) being 
destroyed

~this(0)
~this(42)



Is it a copy? I think the first destructor call is one of `f` in 
`gen` (marked by the comment above)


Re: dmd 2.098.0: version `GLIBC_2.14' not found (required by linux/bin64/dmd)

2021-11-08 Thread jfondren via Digitalmars-d-learn

On Monday, 8 November 2021 at 23:55:02 UTC, kdevel wrote:
In previous versions I used the linux32/dmd with the -m64 
switch in order to generate 64-bit code. But this does not work 
anymore:


   $ linux/bin32/dmd
linux/bin32/dmd: /lib/libc.so.6: version `GLIBC_2.28' not found 
(required by linux/bin32/dmd)


dmd version v2.089.0 should work for you, and you can use that to 
build a newer version of dmd. You can get it from 
http://downloads.dlang.org/releases/2019/


You can use this on an older Linux system to generate more 
compatible binaries, or you can try building static binaries. 
I've got a short guide for LDC on Alpine Linux in a docker 
container at 
https://d.minimaltype.com/index.cgi/wiki?name=statically-linked+binaries


Is it possible to build the compiler and the tools with more 
"backward compatible" glibc version numers like 
memcpy@GLIBC_2.2.5 and fcntl@GLIBC_2.2.5? IIRC this is 
accomplished by using


   asm (".symver memcpy, memcpy@GLIBC_2.2.5");
   asm (".symver fcntl, fcntl@GLIBC_2.2.5");

in the source code.


... I'd hope that the version numbers aren't so meaningless that 
dmd could get away with just lying about them and not have 
horrible problems.


I'd prefer that dmd work out of the box on old Linux systems too, 
but you're probably past EOL in other big ways as well, there. A 
stock CentOS6 system comes with a root privilege escalation vuln 
in sudoedit


dmd 2.098.0: version `GLIBC_2.14' not found (required by linux/bin64/dmd)

2021-11-08 Thread kdevel via Digitalmars-d-learn
In previous versions I used the linux32/dmd with the -m64 switch 
in order to generate 64-bit code. But this does not work anymore:


   $ linux/bin32/dmd
linux/bin32/dmd: /lib/libc.so.6: version `GLIBC_2.28' not found 
(required by linux/bin32/dmd)


The reason for presence of these symbol versions are memcpy and 
fcntl:


   $ nm --with-symbol-versions -D linux/bin64/dmd|grep GLIBC_2.14
U memcpy@GLIBC_2.14

   $ nm --with-symbol-versions -D linux/bin32/dmd|grep GLIBC_2.28
U fcntl@GLIBC_2.28

Is it possible to build the compiler and the tools with more 
"backward compatible" glibc version numers like 
memcpy@GLIBC_2.2.5 and fcntl@GLIBC_2.2.5? IIRC this is 
accomplished by using


   asm (".symver memcpy, memcpy@GLIBC_2.2.5");
   asm (".symver fcntl, fcntl@GLIBC_2.2.5");

in the source code.



Re: Crosscompile to Windows

2021-11-08 Thread Luis via Digitalmars-d-learn
On Monday, 8 November 2021 at 01:16:25 UTC, rikki cattermole 
wrote:


On 08/11/2021 11:34 AM, Imperatorn wrote:

On Sunday, 7 November 2021 at 22:19:08 UTC, russhy wrote:

If i remember correctly, all you have to do is:

    dub build --arch=x86_64-pc-windows-msvc --compiler=ldc2


Is this information on dub.pm? If not, we should add it. 


This is compiler specific.

The arch triple is only supported for ldc. If you tried that 
with gdc it would error out.


It should work with the last version of DMD : 
https://dlang.org/changelog/2.098.0.html#target





Re: Crosscompile to Windows

2021-11-08 Thread rikki cattermole via Digitalmars-d-learn



On 09/11/2021 12:44 PM, Luis wrote:
It should work with the last version of DMD : 
https://dlang.org/changelog/2.098.0.html#target


It won't.

https://github.com/dlang/dub/blob/master/source/dub/compilers/dmd.d#L111


auto ref function parameter causes that non copyable struct is copied?

2021-11-08 Thread tchaloupka via Digitalmars-d-learn

Lets have this code:

```D
import core.lifetime : forward;
import core.stdc.stdio;
import std.algorithm : move;

struct Value(T) {
private T storage;

this()(auto ref T val) {
storage = forward!val;
}

ref inout(T) get() inout {
return storage;
}
}

Value!T value(T)(auto ref T val) {
return Value!T(forward!val);
}

auto ref unwrap(EX)(auto ref EX res) {
return res.get();
}

struct Foo {
int n;
@disable this(this);
~this() { printf("~this(%d)\n", n); }
}

auto gen() {
Foo f;
f.n = 42;
return value(f.move());
}

void main() {
Foo f;
f = gen().unwrap.move;
}
```

As I understand it unwrap in `f = gen().unwrap.move` can't be 
called by ref (as gen() returns rvalue) so it would force the 
`Value` to be copied but as it holds non copyable struct, it 
can't be and so it should end up with a compiler error.


But the code outputs:
```
~this(0)
~this(0)
~this(0)
~this(42) <- this is a copy (that shouldn't exist) being destroyed
~this(0)
~this(42)
```

This could cause unwanted resource cleanup on a seemingly non 
copyable structs.

Bug or feature? :)


Re: Completing C code with D style

2021-11-08 Thread forkit via Digitalmars-d-learn

On Monday, 8 November 2021 at 12:04:26 UTC, forkit wrote:


case 'o' :
result = result.filter!(a => (a % 2 == 1)).array;


oops.

case 'o' :
result = result.filter!(a => (a % 2 != 0)).array;




Re: Does D have any number theory libraries?

2021-11-08 Thread Imperatorn via Digitalmars-d-learn

On Monday, 8 November 2021 at 20:09:26 UTC, Imperatorn wrote:

On Monday, 8 November 2021 at 19:59:35 UTC, Enjoys Math wrote:

In particular what I need are the fast implementations of:

1. The Nth prime number.
2. Prime Omega and/or Mobius function.
3. Works with some type of BigInt.
4. Primorial.
5. Divisors of N.
6. Extended GCD algorithm.

They don't have to be the state-of-the art, but it would be 
nice if they didn't simply do the bruteforce algorithm 
everywhere.


Check out Mir:
https://github.com/libmir

And std.numeric:
https://dlang.org/library/std/numeric.html

Primes:
https://code.dlang.org/packages/math-primes

Möbius:
https://rosettacode.org/wiki/M%C3%B6bius_function#D


GMP:
https://code.dlang.org/packages/gmp-d

Linear algebra:
https://github.com/kaleidicassociates/lubeck

SciD:
https://github.com/DlangScience/scid

https://johanengelen.github.io/ldc/2016/10/11/Math-performance-LDC.html


Re: Does D have any number theory libraries?

2021-11-08 Thread Imperatorn via Digitalmars-d-learn

On Monday, 8 November 2021 at 19:59:35 UTC, Enjoys Math wrote:

In particular what I need are the fast implementations of:

1. The Nth prime number.
2. Prime Omega and/or Mobius function.
3. Works with some type of BigInt.
4. Primorial.
5. Divisors of N.
6. Extended GCD algorithm.

They don't have to be the state-of-the art, but it would be 
nice if they didn't simply do the bruteforce algorithm 
everywhere.


Check out Mir:
https://github.com/libmir

And std.numeric:
https://dlang.org/library/std/numeric.html

Primes:
https://code.dlang.org/packages/math-primes

Möbius:
https://rosettacode.org/wiki/M%C3%B6bius_function#D




Does D have any number theory libraries?

2021-11-08 Thread Enjoys Math via Digitalmars-d-learn

In particular what I need are the fast implementations of:

1. The Nth prime number.
2. Prime Omega and/or Mobius function.
3. Works with some type of BigInt.
4. Primorial.
5. Divisors of N.
6. Extended GCD algorithm.

They don't have to be the state-of-the art, but it would be nice 
if they didn't simply do the bruteforce algorithm everywhere.


Re: Completing C code with D style

2021-11-08 Thread forkit via Digitalmars-d-learn

On Tuesday, 2 November 2021 at 23:45:39 UTC, pascal111 wrote:
Next code originally was a classic C code I've written, it's 
pure vertical thinking, now, I converted it successfully to D 
code, but I think I made no much changes to make it has more 
horizontal thinking style that it seems D programmers care in 
horizontal thinking style. Is there any additions I can make in 
the code to make it suitable with D style or it's fine?




Oh man! some of those answers... whoohah

Here's mine, with a little of that horizontal thinking ;-)

// =

module test;

import std.stdio : write, writeln, writefln, readf;
import std.algorithm : filter, sort;
import std.array: array;

void main()
{
// NOTE: 0 is neither positive or negative, but is considered 
to be even


int[] numbers = [-3, 14, 47, -49, -30, 15, 4, -82, 99, 26, 0];
int[] result;

char answer1;
write("Would you like in list (n=negatives, p=positives, 
b=both)? ");

readf(" %c", answer1);
debug { writeln("You selected ", answer1); }
switch(answer1)
{
case 'n' :
result = numbers.filter!(a => (a < 0)).array;
break;
case 'p' :
result = numbers.filter!(a => (a > 0)).array;
break;
case 'b' :
result = numbers;
break;
default :
writefln("Invalid answer." );
throw new Exception("Bad input!!"); // don't waste my 
time!

}


char answer2;
write("Would you like in list (e=evens, o=odds, b=both)? ");
readf(" %c", answer2);
debug { writeln("You selected ", answer2); }
switch(answer2)
{
case 'e' :
result = result.filter!(a => (a % 2 == 0)).array;
break;
case 'o' :
result = result.filter!(a => (a % 2 == 1)).array;
break;
case 'b' :
break;
default :
writefln("Invalid answer." );
throw new Exception("Bad input!!"); // don't waste my 
time!

}


writeln(result.sort());

}

// =