Thread overview | ||||||
---|---|---|---|---|---|---|
|
November 20, 2015 Compile time strings auto concatenation!? | ||||
---|---|---|---|---|
| ||||
Can DMD frontend optimize string concatenation ``` enum Double(S) = S ~ S; assert(condition, "Text " ~ Double!"+" ~ ___FUNCTION__); ``` to ``` assert(condition, "Text ++_function_name_"); ``` ? |
November 20, 2015 Re: Compile time strings auto concatenation!? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya | On Fri, 20 Nov 2015 20:39:57 +0000, Ilya wrote:
> Can DMD frontend optimize
> string concatenation
> ```
> enum Double(S) = S ~ S;
>
> assert(condition, "Text " ~ Double!"+" ~ ___FUNCTION__);
> ```
>
> to
>
> ```
> assert(condition, "Text ++_function_name_");
>
> ```
> ?
Yes this occurs as part of the constant folding I believe.
$ cat test.d
enum Double(string S) = S ~ S;
void main(string[] args){
assert(args.length, "Text " ~ Double!"+" ~ __FUNCTION__);
}
$ dmd test.d
$ strings test | grep Text
Text ++test.main
|
November 21, 2015 Re: Compile time strings auto concatenation!? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya | On Friday, 20 November 2015 at 20:39:58 UTC, Ilya wrote:
> Can DMD frontend optimize
> string concatenation
> ```
> enum Double(S) = S ~ S;
>
> assert(condition, "Text " ~ Double!"+" ~ ___FUNCTION__);
> ```
>
> to
>
> ```
> assert(condition, "Text ++_function_name_");
>
> ```
> ?
If you really want to make sure it is concatenated at compile time, just do this:
...
enum CT(alias S) = S;
assert(condition, CT!("Text " ~ Double!"+" ~ ___FUNCTION__)); // Must be evaluated at CT no matter what
|
November 21, 2015 Re: Compile time strings auto concatenation!? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya | On Friday, 20 November 2015 at 20:39:58 UTC, Ilya wrote:
> Can DMD frontend optimize
> string concatenation
> ```
> enum Double(S) = S ~ S;
>
> assert(condition, "Text " ~ Double!"+" ~ ___FUNCTION__);
> ```
>
> to
>
> ```
> assert(condition, "Text ++_function_name_");
>
> ```
> ?
At least for string (and array?) literals, it's now guaranteed to happen at compile time. It was changed in order to get rid of the error-prone "concatenation by juxtaposition" feature inherited from C. I'm not sure it also applies to non-literals, though.
|
Copyright © 1999-2021 by the D Language Foundation