Thread overview
Interfacing D with C: Arrays and Functions (Arrays Part 2)
Apr 28, 2020
Mike Parker
Apr 28, 2020
David Gileadi
Apr 28, 2020
Mike Parker
Apr 28, 2020
Seb
Apr 29, 2020
Walter Bright
April 28, 2020
I've finally gotten around to publishing the next article in my D and C series on the D blog. This is the second post about arrays, focusing on properly declaring in D functions from C that accept array parameters.

The blog:
https://dlang.org/blog/2020/04/28/interfacing-d-with-c-arrays-and-functions-arrays-part-two/

Reddit:
https://www.reddit.com/r/programming/comments/g9o3wi/interfacing_d_with_c_arrays_and_functions_arrays/

HN:
https://news.ycombinator.com/item?id=23007409
April 28, 2020
On 4/28/20 7:45 AM, Mike Parker wrote:
> I've finally gotten around to publishing the next article in my D and C series on the D blog. This is the second post about arrays, focusing on properly declaring in D functions from C that accept array parameters.
> 
> The blog:
> https://dlang.org/blog/2020/04/28/interfacing-d-with-c-arrays-and-functions-arrays-part-two/ 

..

Another great article!

Nit: in the following paragraph, did you really mean to say *are*?

> But again, even D’s safety features are 100% foolproof when calling C functions from D.
April 28, 2020
On Tuesday, 28 April 2020 at 15:26:49 UTC, David Gileadi wrote:

> Another great article!
>
> Nit: in the following paragraph, did you really mean to say *are*?
>
>> But again, even D’s safety features are 100% foolproof when calling C functions from D.

Thanks and thanks!
April 28, 2020
On Tuesday, 28 April 2020 at 14:45:37 UTC, Mike Parker wrote:
> I've finally gotten around to publishing the next article in my D and C series on the D blog. This is the second post about arrays, focusing on properly declaring in D functions from C that accept array parameters.
>
> The blog:
> https://dlang.org/blog/2020/04/28/interfacing-d-with-c-arrays-and-functions-arrays-part-two/
>
> Reddit:
> https://www.reddit.com/r/programming/comments/g9o3wi/interfacing_d_with_c_arrays_and_functions_arrays/
>
> HN:
> https://news.ycombinator.com/item?id=23007409

Great article as always!
Maybe this article and conversation revives the interest in a very much related area: array mangling still doesn't work for extern(C++).
Example:

---
extern(C++) void foo(int[] arr);
void main()
{
   foo([1, 2]);
}
---

// Internal Compiler Error: type int[] cannot be mapped to C++

There have been multiple attempts to fix this, the latest one is https://github.com/dlang/dmd/pull/8120.
Now, that dmd comes with the (experimental) -H flag for C++ header generation, maybe there's more interest in making D arrays/strings work seamlessly between D and C++?
April 28, 2020
On 4/28/20 10:45 AM, Mike Parker wrote:
> I've finally gotten around to publishing the next article in my D and C series on the D blog. This is the second post about arrays, focusing on properly declaring in D functions from C that accept array parameters.
> 
> The blog:
> https://dlang.org/blog/2020/04/28/interfacing-d-with-c-arrays-and-functions-arrays-part-two/ 
> 
> 
> Reddit:
> https://www.reddit.com/r/programming/comments/g9o3wi/interfacing_d_with_c_arrays_and_functions_arrays/ 
> 
> 
> HN:
> https://news.ycombinator.com/item?id=23007409

Nice! I was ready to respond with what turns out to be the punchline here -- using ref ;)

That little bit of sorcery is an excellent trick that can make a world of difference when it comes to compiler-enforced correctness.

-Steve
April 29, 2020
On 4/28/2020 9:12 AM, Seb wrote:
> // Internal Compiler Error: type int[] cannot be mapped to C++
> 
> There have been multiple attempts to fix this, the latest one is https://github.com/dlang/dmd/pull/8120.
> Now, that dmd comes with the (experimental) -H flag for C++ header generation, maybe there's more interest in making D arrays/strings work seamlessly between D and C++?

They're never going to work seamlessly between D and C++ because C++ does not have a standard compatible type for it. We discussed this at length at DConf 2019.