auto scope question?

Ali Çehreli acehreli at yahoo.com
Tue Oct 25 22:25:37 UTC 2022


On 10/25/22 15:07, WhatMeWorry wrote:

 > auto screen = executeShell(cmdLine);
 > auto s;

That can't work because there is no information to infer the type of 
's'. Judging from the return type of getPath, perhaps it's string[]:

string[] s;

This is the question we should answer first: What should happen when 
executeShell fails?

a) It is an error; the program should not continue. Then we can use 
'enforce' (this is my way of coding):

string[] getPath(string cmdLine)
{
     import std.exception : enforce;

     auto screen = executeShell(cmdLine);
     enforce(screen.status == 0, format!"%s failed:\n%s"(cmdLine, 
screen.output));

     writeln("screen.output = ", screen.output);
     auto s = screen.output.findSplit("REG_SZ");
     writeln("s[0] = ", s[0]);
     writeln("s[1] = ", s[1]);
     writeln("s[2] = ", s[2]);

     return (s.split(';'));
}

b) It is not an error; getPath() should return empty array:

string[] getPath(string cmdLine)
{
     string[] result;
     auto screen = executeShell(cmdLine);

     if (screen.status != 0)
     {
         writeln(cmdLine, " failed");
         return null;  // <-- HERE (null converts to any array type)
     }
     else
     {
         // ...
         // Now, 'return' is where 's' is defined:
         return (s.split(';'));
     }
}

Ali



More information about the Digitalmars-d-learn mailing list