I need to pass an array of strings to an extern C function. I’m starting by working out how to populate such an array in chapel.
This code:
use CTypes;
param count: int = 10;
var x: c_array(c_int, count);
if x.eltType == c_int then writeln("x is an array of c_int with ", count, " elements.");
var y: c_array(c_char, count);
if y.eltType == c_char then writeln("y is an array of c_char with ", count, " elements.");
y[0] = 's';
fails to compile:
c_array.chpl:10: error: Cannot assign to int(8) from string
I think this may be a mismatch between char representation in chapel and in C (although obviously I’m not actually callin C yet.
Chapel decided not to support a distinct char type from string as in C since in a Unicode world, the notion of a one-byte character seems dated and old-fashioned
Simultaneously, we decided to let string literals be defined using either "…" or '...' to meet users coming from various languages where single or double quotes might be used, and to permit strings like '"He said what?", she said' and "I don't know what you mean" without resorting to escape characters to access the other quote type (of course this is still necessary for a string that wants to contain both " and '
The unfortunate downside of this combination of choices is that a C user may understandably assume that 'c' is a character. It occurs to me that with more work we could add specialized error messages to help guide users who hit such cases, e.g.
c_array.chpl:10: error: Cannot assign to int(8) from string
c_array.chpl:10: note: In Chapel 's' is a string literal rather than a char as in C
If anyone wanted to advocate for this change, opening an issue for it on our GitHub repo would be very reasonable I think (how much work it would be to cover all possible cases where someone makes the mistake is unclear to me, though).