Re: Using D within a rust codebase

2020-07-26 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 26 July 2020 at 21:18:19 UTC, powerboat9 wrote:
I have an existing rust project, and I'm trying to rewrite part 
of it in D. However, I'm not sure how to get rust -> dlang 
interop working. I've looked into rust -> c -> dlang interop, 
but I'm not sure how to get c -> dlang interop working either.


Here's the basic approach:

1. Write an `extern(C)` function in D.
2. Write a corresponding declaration of that function in C 
(typically, in a header file).
3. Use your D and C compilers to separately compile your D and C 
source files, respectively.

4. Use your D compiler to link the resulting object files.

For example, given these source files:

--- lib.d
extern(C) void hello()
{
import std.stdio: writeln;
writeln("Hello from D!");
}

--- lib.h
void hello();

--- main.c
#include "lib.h"

void main()
{
hello();
}

...you can compile them with the following commands:

$ dmd -c lib.d # produces lib.o
$ gcc -c main.c# produces main.o
$ dmd main.o lib.o # produces main

You should now be able run `./main` and see it print "Hello from 
D!"


Re: Template error with gdc-10 but not with latest dmd and ldc

2020-07-26 Thread Per Nordlöw via Digitalmars-d-learn

On Sunday, 26 July 2020 at 21:48:09 UTC, Per Nordlöw wrote:

I see. Thanks.


The code

long add_long_n0(alias T=void)(long x) { return x + 0; }

should be

long add_long_n0(T=void)(long x) { return x + 0; }

. My mistake.

Thanks.


Re: Template error with gdc-10 but not with latest dmd and ldc

2020-07-26 Thread Per Nordlöw via Digitalmars-d-learn

On Sunday, 26 July 2020 at 20:32:55 UTC, Iain Buclaw wrote:

This feature?

https://dlang.org/changelog/2.087.0.html#template_alias_matches_basic_types


I see. Thanks.


Using D within a rust codebase

2020-07-26 Thread powerboat9 via Digitalmars-d-learn
I have an existing rust project, and I'm trying to rewrite part 
of it in D. However, I'm not sure how to get rust -> dlang 
interop working. I've looked into rust -> c -> dlang interop, but 
I'm not sure how to get c -> dlang interop working either.


Re: Template error with gdc-10 but not with latest dmd and ldc

2020-07-26 Thread Iain Buclaw via Digitalmars-d-learn

On Sunday, 26 July 2020 at 19:27:13 UTC, rikki cattermole wrote:
2.066.0 to 2.078.1: Failure with output: onlineapp.d(7): Error: 
template instance add_long_n0!void does not match template 
declaration add_long_n0(alias T = void)(long x)
2.079.1 to 2.086.1: Failure with output: onlineapp.d(7): Error: 
template instance `add_long_n0!void` does not match template 
declaration `add_long_n0(alias T = void)(long x)`

Since  2.087.1: Success and no output


This feature?

https://dlang.org/changelog/2.087.0.html#template_alias_matches_basic_types


Re: Template error with gdc-10 but not with latest dmd and ldc

2020-07-26 Thread Per Nordlöw via Digitalmars-d-learn

On Sunday, 26 July 2020 at 19:27:13 UTC, rikki cattermole wrote:

Old frontend:

Up to  2.060  : Failure with output: onlineapp.d(2): Error: 
valid attribute identifiers are @property, @safe, @trusted, 
@system, @disable not @nogc

2.061   to 2.065.0: Failure with output:
-
onlineapp.d(2): Error: user defined attributes cannot appear as 
postfixes
onlineapp.d(2): Error: semicolon expected following function 
declaration

onlineapp.d(2): Error: Declaration expected, not 'return'
onlineapp.d(2): Error: unrecognized declaration
-

2.066.0 to 2.078.1: Failure with output: onlineapp.d(7): Error: 
template instance add_long_n0!void does not match template 
declaration add_long_n0(alias T = void)(long x)
2.079.1 to 2.086.1: Failure with output: onlineapp.d(7): Error: 
template instance `add_long_n0!void` does not match template 
declaration `add_long_n0(alias T = void)(long x)`

Since  2.087.1: Success and no output


I don't understand.

I removed the qualifiers but still get the same error from this 
unqualified code:



long add_long_n0(alias T=void)(long x) { return x + 0; }

int main(string[] args) {
long long_sum = 0;
long_sum += add_long_n0!(void)(0);
return cast(int)long_sum;
}



Re: Template error with gdc-10 but not with latest dmd and ldc

2020-07-26 Thread rikki cattermole via Digitalmars-d-learn

Old frontend:

Up to  2.060  : Failure with output: onlineapp.d(2): Error: valid 
attribute identifiers are @property, @safe, @trusted, @system, @disable 
not @nogc

2.061   to 2.065.0: Failure with output:
-
onlineapp.d(2): Error: user defined attributes cannot appear as postfixes
onlineapp.d(2): Error: semicolon expected following function declaration
onlineapp.d(2): Error: Declaration expected, not 'return'
onlineapp.d(2): Error: unrecognized declaration
-

2.066.0 to 2.078.1: Failure with output: onlineapp.d(7): Error: template 
instance add_long_n0!void does not match template declaration 
add_long_n0(alias T = void)(long x)
2.079.1 to 2.086.1: Failure with output: onlineapp.d(7): Error: template 
instance `add_long_n0!void` does not match template declaration 
`add_long_n0(alias T = void)(long x)`

Since  2.087.1: Success and no output


Template error with gdc-10 but not with latest dmd and ldc

2020-07-26 Thread Per Nordlöw via Digitalmars-d-learn

The code example


long add_long_n0(alias T=void)(long x) @safe pure nothrow @nogc { 
return x + 0; }


int main(string[] args)
{
long long_sum = 0;
long_sum += add_long_n0!(void)(cast(long)0);
return cast(int)long_sum;
}


compiles without errors with dmd and ldc. But with gdc-10 as

gdc -c linear_t.d

it errors as

linear_t.d:6:17: error: template instance add_long_n0!() does not 
match template declaration add_long_n0(alias T = void)(long x)

6 | long_sum += add_long_n0!()(cast(long)0);
  | ^


Why can't LDC handle such a trivial template example?


Re: Help with Ranges

2020-07-26 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/26/20 3:10 AM, Charles wrote:
Suppose I have the following line of code where arr is an array, 
doSomething is some predicate that does a lot of processing on each 
element, sort must come after the mapping, and there are more operations 
done to the range after sort:


arr.map!doSomething.sort. ...;

Sort fails to instantiate because the range it's receiving doesn't 
support element swapping. This may and might be resolved by calling array:


arr.map!doSomething.array.sort. ...;

However, this might trigger an allocation, and there's still more to do! 
Is there something I'm missing with regards to ranges that could help me 
make the first line work without using array, or is it more of an issue 
with my code's design?


That is what you need to do.

A map that returns an lvalue would be sortable, but you would be sorting 
the processed elements, and probably not the original elements.


I have found this handy tool quite useful in my code where I need a 
temporary array:


// creates a concrete range (std.container.array.Array range) out of the
// original range that is eagerly fetched, and then can be processed, 
without

// allocating extra garbage on the heap.
auto concreteRange(Range)(Range r)
{
import std.range : ElementType;
import std.container.array : Array;
return Array!(ElementType!Range)(r)[];
}

Slicing an Array will keep the reference count correctly, and destroy 
the memory automatically after you're done using it. So it's perfect for 
temporary arrays in pipelining.


-Steve


Re: Static link of glfw3 library fails for me

2020-07-26 Thread John Burton via Digitalmars-d-learn

On Sunday, 26 July 2020 at 10:41:27 UTC, Mike Parker wrote:

On Sunday, 26 July 2020 at 08:28:29 UTC, John Burton wrote:



And I get the following errors from the link :-

lld-link: error: undefined symbol: __GSHandlerCheck
lld-link: error: undefined symbol: __security_check_cookie
lld-link: error: undefined symbol: __security_cookie


I believe that's because the GLFW library was compiled with the 
Microsoft compiler's /GS option:


https://docs.microsoft.com/en-us/cpp/build/reference/gs-buffer-security-check?view=vs-2019

The __security_* functions are MS extensions to the C standard 
library. A quick search suggests you should try linking with 
bufferoverflowU.lib. Either that or compile GLFW with /GS- to 
turn off the security checks.


This is one reason I gave on on static linking pre-built C 
binaries long ago. I use the static bindings with import 
libraries, but I don't touch static libs unless I compile them 
myself.


Thank you. I'll look into this.
I wanted a single statically linked binary for an application I 
had in mind so thought I'd try this out. It's nice just to be 
able to send a single binary without needing to have an installer 
or copy multiple files for some use cases. This is the main 
reason I quite like go, not so much for the language but that 
things like "gitea" can be just one single binary and nothing 
else.


I can rebuild glfw I guess, that's not a problem (but makes it 
harder for people if I want to share my code of course). Perhaps 
I ought to try this.


I understand, as mentioned in your other reply, that I'll have to 
link with all the other dependencies of glfw too. I 
oversimplified my example a bit too much :)


Re: Static link of glfw3 library fails for me

2020-07-26 Thread Mike Parker via Digitalmars-d-learn

On Sunday, 26 July 2020 at 08:28:29 UTC, John Burton wrote:


versions "BindGLFW_Static"
libs "glfw3"
lflags "-L..\\work\\3rdparty\\lib"




And by the way, you're going to need to link more libs than glfw3 
for a static link. You'll need all of its dependencies, as well 
(OpenGL32.lib, gdi32.lib, etc).


Re: Static link of glfw3 library fails for me

2020-07-26 Thread Mike Parker via Digitalmars-d-learn

On Sunday, 26 July 2020 at 08:28:29 UTC, John Burton wrote:



And I get the following errors from the link :-

lld-link: error: undefined symbol: __GSHandlerCheck
lld-link: error: undefined symbol: __security_check_cookie
lld-link: error: undefined symbol: __security_cookie


I believe that's because the GLFW library was compiled with the 
Microsoft compiler's /GS option:


https://docs.microsoft.com/en-us/cpp/build/reference/gs-buffer-security-check?view=vs-2019

The __security_* functions are MS extensions to the C standard 
library. A quick search suggests you should try linking with 
bufferoverflowU.lib. Either that or compile GLFW with /GS- to 
turn off the security checks.


This is one reason I gave on on static linking pre-built C 
binaries long ago. I use the static bindings with import 
libraries, but I don't touch static libs unless I compile them 
myself.


Static link of glfw3 library fails for me

2020-07-26 Thread John Burton via Digitalmars-d-learn

I'm trying to replicate a program I make in C++ using D.
I am using the ldc2 compiler and want to *static* link in the 
glfw library.
Following the docs I have an dub.sdl file that looks like the one 
below.


The library I'm linking with is the vs2019 one from the GLFW zip 
file from
their website. My program isn't work showing, it just calls 
glfwInit, creates a
windows, and does a basic event loop. (This is a simplified test 
made when

I couldn't get my real program to link)

However I get link errors shown below.

Am I doing this wrong?
Am I using the wrong library to link with? (And if so which 
should I use).

Am I missing any options etc?
Or will this just not work with ldc2?

Thanks for any help!

My dub.sdl file looks like this :-

name "game"
description "Test Project"
authors "Me"
copyright "Copyright © 2020, Me"
license "proprietary"
dependency "bindbc-glfw" version="~>0.10.0"
versions "BindGLFW_Static"
libs "glfw3"
lflags "-L..\\work\\3rdparty\\lib"

And I get the following errors from the link :-

lld-link: error: undefined symbol: __GSHandlerCheck
lld-link: error: undefined symbol: __security_check_cookie
lld-link: error: undefined symbol: __security_cookie




Help with Ranges

2020-07-26 Thread Charles via Digitalmars-d-learn
Suppose I have the following line of code where arr is an array, 
doSomething is some predicate that does a lot of processing on 
each element, sort must come after the mapping, and there are 
more operations done to the range after sort:


arr.map!doSomething.sort. ...;

Sort fails to instantiate because the range it's receiving 
doesn't support element swapping. This may and might be resolved 
by calling array:


arr.map!doSomething.array.sort. ...;

However, this might trigger an allocation, and there's still more 
to do! Is there something I'm missing with regards to ranges that 
could help me make the first line work without using array, or is 
it more of an issue with my code's design?