Sujet : Re: C23 thoughts and opinions
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 16. Jun 2024, 00:20:34
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4l7k1$3m349$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
User-Agent : Mozilla Thunderbird
On 15/06/2024 23:39, Lawrence D'Oliveiro wrote:
On Sat, 15 Jun 2024 20:27:41 +0100, bart wrote:
The "+" is used for compile-time string/data-string concatenation.)
Why didn’t you follow the C convention of implicit concatenation, just by
placing literals next to each other?
Why is that better?
I did actually have that, but it wasn't as useful. It could only work at the lexical level with actual string literals, for a start.
As it is now I can do this:
const x = "abc"
const y = "def"
const z = x + y # "abcdef"
These are named constants with proper scope, which are only resolved in a later pass. It also applies to strings created by an embedded file:
s := "(" + sinclude("help.txt") + ")"
I can use parentheses and it will still work:
const cond = ...
print (cond | "abc" | "def") + "xyz"
It will display 'abcxyz' or 'defxyz' depending on 'cond', which is known at compile-time.
I could choose to implement "*" also ...
(I've just spent 10 minutes doing that)
... so that I can do this, where having proper operators comes in useful:
"A" + "B" * 5 ABBBBB
("A" + "B") * 5 ABABABABAB
Here is a use-case:
const cols = 80
println "-" * cols # output divider line
This in a lower level language where strings are not first class types.
How C does it is a hack that was fine for 1972.