fut
fut copied to clipboard
Printing concatenated results
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.
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);
The concatenation operator is only partially implemented for C. I will either implement it fully or block it.
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 Is this still an issue in 2023?
This was low-priority because of the string interpolation workaround.
Now that b29f67cd5764c86a811dd7bc912d81262eda35ac restricts concatenation to strings, it was trivial to implement. Thanks!
@pfusik Thank you!