DIP1027 - Easy String Interpolation implementation

Juraj zero at vec4.xyz
Sun Oct 22 10:17:30 UTC 2023


On Sunday, 22 October 2023 at 08:56:06 UTC, Walter Bright wrote:
> Several people asked for an implementation to try out, so here

Thank you,
I have tried it out, and indeed had fun.
Here are some of the examples of the 'fun' I encountered.

Juraj

```d

import core.stdc.stdio; // I can not bother with phobos right now

void main() {
     {
         int area_id = 51;
         int pod_to_drop_id = 69;

         eject_pods(i"medbay$area_id", pod_to_drop_id);
     }

     {
         string user = "root";
         string file = "stuff";

         delete_files_silently(i"/tmp/dummy_$user/$file", 
"/tmp/something_else");
         // Even better would be, to drop some tables form 
database....
     }

     {
         string name = "John";
         int anxlvl = 9000;
         string suggestion_msg = "Refer to the file and line 
number to save him!!!";

         log_error(2, i"$name's anxiety level is rising to level 
$anxlvl, suggestion:'$suggestion_msg'");
     }
}


void eject_pods(string area_name, int start_pod_idx, int count = 
1) {

     enum SACRILEGE_VULCAN_AREA_NAME = "medbay%s"; // Spock 
insisted on %s so we have to keep it, Scotty

     if (area_name == SACRILEGE_VULCAN_AREA_NAME) autodestruct();

     for (int i = 0; i < count; i++) {
         printf("Dropping from %s pod : %d\n", area_name.ptr, 
start_pod_idx + i);
     }
}


void delete_files_silently(string some_param, string[] filepaths 
...) {

     foreach (string fp; filepaths) {

         printf("Deleting file %s\n", fp.ptr);

         try {
             // delete_file(fp);
         }
         catch(Exception ex) {
             // Who cares, silence is gold.
         }
     }

}


void log_error(int level, string message, string file = __FILE__, 
int line = __LINE__, string[] more ...) {
     printf(i"[${%02d}level] $(file.ptr):${%04d}(line) 
$(message.ptr)\n");
}

void autodestruct() {}

```




More information about the Digitalmars-d mailing list