fut icon indicating copy to clipboard operation
fut copied to clipboard

Printing concatenated results

Open JoshuaWierenga opened this issue 3 years ago • 3 comments

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.

JoshuaWierenga avatar Oct 15 '21 06:10 JoshuaWierenga

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 avatar Oct 15 '21 07:10 pfusik

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

pfusik avatar Oct 15 '21 07:10 pfusik

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 avatar Oct 15 '21 08:10 pfusik

@pfusik Is this still an issue in 2023?

danon avatar Aug 09 '23 14:08 danon

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

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

pfusik avatar Aug 09 '23 15:08 pfusik

@pfusik Thank you!

danon avatar Aug 09 '23 15:08 danon