www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Question re specific implicit type coercion

reply Don Allen <donaldcallen gmail.com> writes:
I am calling a C function, described in D as

extern (C) GtkWidget* gtk_menu_item_new_with_label 
(immutable(char)*);

I call it like this

accounts_menu_item = gtk_menu_item_new_with_label("New account 
(Ctrl-n)");

The type of the string literal in the call is immutable(char)[]. 
The type of the parameter is a pointer to immutable characters.

This setup works.

My question is about the documentation vs. what is really going 
on here.

Section 12.17 of the Language Reference does not indicate any 
circumstance in which a dynamic array, which the literal is, is 
implicitly coerced to a pointer. I realize that this is a special 
case -- the callee is a C function -- that might not be covered 
by this section. So I looked at Section 32.3, Chapter 32 being 
"Interfacing to C". 32.3 contains a table of correspondences 
between D and C types. The entry for the D type T[] indicates no 
correspondence in C.

So at least my reading of the documentation suggests that my code 
should not compile. And yet it does and works, which leads me to 
believe there is a documentation error that ought to be reported.

Before doing so, I decided to send this message so those of you 
who know D a lot better than I do can tell me what I've missed or 
gotten wrong, if that is the case.

Thanks --
/Don
Oct 18 2021
parent reply Paul Backus <snarwin gmail.com> writes:
On Monday, 18 October 2021 at 15:04:11 UTC, Don Allen wrote:
 Section 12.17 of the Language Reference does not indicate any 
 circumstance in which a dynamic array, which the literal is, is 
 implicitly coerced to a pointer.
This is a special case for string literals, covered in [section 10.23.7][1]. [1]: https://dlang.org/spec/expression.html#string_literals
Oct 18 2021
parent reply Don Allen <donaldcallen gmail.com> writes:
On Monday, 18 October 2021 at 15:34:45 UTC, Paul Backus wrote:
 On Monday, 18 October 2021 at 15:04:11 UTC, Don Allen wrote:
 Section 12.17 of the Language Reference does not indicate any 
 circumstance in which a dynamic array, which the literal is, 
 is implicitly coerced to a pointer.
This is a special case for string literals, covered in [section 10.23.7][1]. [1]: https://dlang.org/spec/expression.html#string_literals
Yes. Thank you.
Oct 18 2021
parent Don Allen <donaldcallen gmail.com> writes:
On Monday, 18 October 2021 at 15:58:35 UTC, Don Allen wrote:
 On Monday, 18 October 2021 at 15:34:45 UTC, Paul Backus wrote:
 On Monday, 18 October 2021 at 15:04:11 UTC, Don Allen wrote:
 Section 12.17 of the Language Reference does not indicate any 
 circumstance in which a dynamic array, which the literal is, 
 is implicitly coerced to a pointer.
This is a special case for string literals, covered in [section 10.23.7][1]. [1]: https://dlang.org/spec/expression.html#string_literals
Yes. Thank you.
I do think it would be helpful, even essential, to have a link in Section 12.17 to the section you cite. The Language Reference is just that, a reference, not a tutorial and it should not be a requirement to read it from cover to cover. Strings and string literals are arrays and 12.17 purports to cover the implicit type conversions among the various array types, but currently leaves out this special case and should not, in my opinion. I will file an issue making this suggestion.
Oct 18 2021