with and shadowing variables

Anonymouse asdf at asdf.net
Mon Jul 23 12:34:27 UTC 2018


On Monday, 23 July 2018 at 12:01:19 UTC, Jim Balter wrote:
> Do you have an actual case where it was a problem, as opposed 
> to a contrived example with semantically empty identifiers? I 
> recently saw another comment objecting to `with` altogether as 
> being obfuscating because you can't tell which symbols are 
> qualified by the symbol in the with clause, when the 
> obfuscation was  clearly due to the meaningless names in the 
> poster's example.

Copy/pasted but changed some bits for clarity.

struct IRCServer
{
     // ...
     string prefixchars;
     string prefixes;  // <--
}

struct IRCBot
{
     // ...
     IRCServer server;
}

struct IRCParser
{
     // ...
     IRCBot bot;
}

IRCParser parser;

// string content == "EXCEPTS INVEX PREFIX=(Yqaohv)!~&@%+";

foreach (entry; content.splitter(" "))
{
     // Roughly rewritten splitting
     auto split = entry.findSplit("=");
     string key = split[0];
     string value = split[2];

     with (parser.bot.server)
     switch (key)
     {
     case "PREFIX":
         // PREFIX=(Yqaohv)!~&@%+
         import std.format : formattedRead;

         string modes;
         string prefixes;  // <--
         value.formattedRead("(%s)%s", modes, prefixes);

         foreach (immutable i; 0..modes.length)
         {
             prefixchars[prefixes[i]] = modes[i];  // 
parser.bot.server.prefixchars
             prefixes ~= modes[i];  // <-- accidental local 
prefixes instead of parser.bot.server.prefixes
         }
         break;

     // ...

     default:
         break;
}

https://github.com/zorael/kameloso/blob/93002da193eac2dfbfeb6c8756feb2d74a345530/source/kameloso/irc.d#L1887


More information about the Digitalmars-d mailing list