-
Notifications
You must be signed in to change notification settings - Fork 419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What should happen when casting a c_ptr to string? #22901
Comments
I'd lean towards it being a compilation error, but how feasible that is depends on what tests fail when we try it. |
If we made it an error (option 1), is the thing a user would need to do be to use one of the If we supported it (option 4), is the idea that we would make a deep copy of the buffer as we create the string? From a productivity standpoint, option 1 seems appealing, but from a safety/sanity standpoint, option 4 seems like the better thing to do, at least in the short-term (assuming my guess above is correct). |
I think you have the option order reversed here, maybe? I think you swapped 1 and 4 based on the descriptions of the behavior. If we made it an error (option 1), then assuming the user meant to print a string they would use one of the
Speaking to option 4 (allowing the cast), yes, the cast would basically just be sugar for However, in the issue I mentioned in the OP #21052, @e-kayrakli expressed a pretty strong aversion to allowing the cast to string to succeed, but maybe I read too much into the comments there. |
Oops, yes, I edited it to fix.
This issue is just focused on what should happen with the cast itself, though, isn't it? (this was also confusing to me in options 2 and 3 which talk about printing rather than the cast). In terms of casting, personally, I don't think there's a world in which casting a c pointer to a Chapel string should give you a string containing the pointer's address. Are there compelling cases in the test/module code that would suggest taking one approach or the other, and how widespread are they? With respect to printing, note that we could potentially support write()ing a const myCharStar = getCharStarFromC();
writeln(myCharStar); which might be a nice productivity boost (no need to convert it to a string manually). OTOH, it might be a little weird to do this so specially for char*s... |
Yes, sorry for mixing the printing in with it, that was just the example where it tends to come up the most... I have this thing, I want to print it to the console, I'll cast it to a string! The result of that cast is what we're really talking about here.
The majority have been replaced already with the PR to deprecate I think it would be convenient if we could write a |
I don't think |
I wouldn't describe it as a weird specialization per se. When defining a new type in Chapel, a type author can define how it's printed out by writeln() (formerly by defining Most of CType's types (c_int, etc.) have Chapel equivalents that Chapel knows about, so automatically get this behavior. c_array and c_ptr don't, so it's reasonable to have them define writeThis overloads (or equivalents in the new world, which I haven't learned yet). And I think it's also reasonable to have some c_ptr types print differently than others (or not support printing at all) since that seems similar to C's special treatment of In saying all this, I'm not saying you need to own it, just saying that I think it's a logical and reasonable thing for CTypes and c_ptr(c_char) to do. |
I'm fine with the notion of casting a pointer to a string and having it yield an address. Whatever we choose in that department, I don't think |
Specifically, when casting a
c_ptr
orc_ptrConst
of element typec_char
,c_uchar
, orc_schar
.Current behavior for
c_string
is to convert to a string, however in the past we've tried to deprecate castingc_string
tostring
[https://github.com//pull/13942], and recently deprecated the universal casting of anything tostring
[https://github.com//pull/22068].In recent work to deprecate
c_string
[https://github.com//pull/22622], the question once again came up as to what the behavior should be. What should the result of casting ac_ptr
orc_ptrConst
of element typec_char
,c_uchar
, orc_schar
to string be?c_string
in the past)example program:
Where I am leaning towards options 2 or 3. Option 1 would be a breaking change and seems extreme. Option 4 is obviously my least favorite and unlikely to be very popular based on #21052
The text was updated successfully, but these errors were encountered: