www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compile time strings auto concatenation!?

reply Ilya <ilyayaroshenko gmail.com> writes:
Can DMD frontend optimize
  string concatenation
```
enum Double(S) = S ~ S;

assert(condition, "Text " ~ Double!"+" ~ ___FUNCTION__);
```

to

```
assert(condition, "Text ++_function_name_");

```
?
Nov 20 2015
next sibling parent Justin Whear <justin economicmodeling.com> writes:
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
Nov 20 2015
prev sibling next sibling parent Tofu Ninja <emmons0 purdue.edu> writes:
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
Nov 21 2015
prev sibling parent Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
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.
Nov 21 2015