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

Printing concatenated results #36

Closed
JoshuaWierenga opened this issue Oct 15, 2021 · 6 comments
Closed

Printing concatenated results #36

JoshuaWierenga opened this issue Oct 15, 2021 · 6 comments
Assignees
Labels

Comments

@JoshuaWierenga
Copy link

Are there any plans for more general concatenation support when going to languages without proper support like c?
This came up while trying Console.Write as Console.Write(int + string) gives fputs(int + string, stdout) and so relies on c to handle it which of course it won't do. This doesn't happen when the int and string are printed separately as Console.Write(int) will correctly give printf("%d", int). Is printing different types separately just the intended way or is this a bug?

Provided this is reasonable to fix, printf(%d%s, int, string) should work fine and generalise to other combinations but I haven't checked what the support for format strings is like since most of the supported languages don't even have them.

@pfusik
Copy link
Collaborator

pfusik commented Oct 15, 2021

Use string interpolation:

int i = 42;
string s = "foo";
Console.WriteLine($"{i}{s}");

C translation:

int i = 42;
const char *s = "foo";
printf("%d%s\n", i, s);

@pfusik pfusik self-assigned this Oct 15, 2021
@pfusik
Copy link
Collaborator

pfusik commented Oct 15, 2021

The concatenation operator is only partially implemented for C. I will either implement it fully or block it.

@pfusik
Copy link
Collaborator

pfusik commented Oct 15, 2021

As a temporary solution, I've added this cito error:

String concatenation not supported when targeting c

My first thought was to remove the concatenation operator from the language. It has several shortcomings:

  • It's overloaded with the addition. The reader needs to consult the types of operands. It's hard to grep for.
  • It has no obvious C translation.
  • "str + i" is the C syntax to offset a pointer.
  • It can be replaced by string interpolation. String interpolation is more concise than concatenation with string literals.
  • String interpolation supports explicit formats. Concatenation has implicit formatting, which could be problematic for floats.

There are however two reasons to keep it:

  • It allows breaking long string literals into multiple lines, with familiar syntax.
  • For consistency with the append operator (+=), which is useful.

@pfusik pfusik added the C label Nov 5, 2021
@danon
Copy link

danon commented Aug 9, 2023

@pfusik Is this still an issue in 2023?

@pfusik pfusik closed this as completed in 25a0675 Aug 9, 2023
@pfusik
Copy link
Collaborator

pfusik commented Aug 9, 2023

This was low-priority because of the string interpolation workaround.

Now that b29f67c restricts concatenation to strings, it was trivial to implement. Thanks!

@danon
Copy link

danon commented Aug 9, 2023

@pfusik Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants