Skip to content
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

Make conversions from char* to String explicit #11258

Open
Ivorforce opened this issue Nov 28, 2024 · 0 comments
Open

Make conversions from char* to String explicit #11258

Ivorforce opened this issue Nov 28, 2024 · 0 comments

Comments

@Ivorforce
Copy link

Describe the project you are working on

#11249 to optimize strings.

Also there's related #11257, with a similar proposal for StringName conversions in general.

Describe the problem or limitation you are having in your project

Strings are currently implicitly created from char *:

bool test(char *inp) {
	String s = inp;
	// rhs is implicitly converted to String before concatenation, leading to unnecessary allocation.
	String("b") + U"";
}

This is leading to many cases of implicit conversions to String for all of String's wide array of functions.

The main problem with allowing this instead of making conversions implicit is that it takes attention away from the programmer. I have previously described how this has specifically lead to 40k lines of shader code being parsed multiple times unnecessarily. If the conversion was explicit, it is more likely that conversion would only have happened once.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Make conversion from char* to String explicit:

explicit String(const char *p_str);

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Callers will have to adapt:

// Illegal
String s = "Test";
// Becomes
String s("Test");
// Illegal
register_string_name("Test")
// Becomes
register_string_name(String("Test"))

Macros and other convenience helperscshould probably be adjusted to accept string literals:

// D_METHOD should still support string literals.
ClassDB::bind_method(D_METHOD("poll"), &StreamPeerTLS::poll);

If this enhancement will not be used often, can it be worked around with a few lines of script?

Nope.

Is there a reason why this should be core and not an add-on in the asset library?

It's core.

@adamscott adamscott changed the title Make conversions from char* to String explicit Make conversions from char* to String explicit Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants