On Tuesday, 27 October 2015 at 15:06:07 UTC, TheFlyingFiddle wrote:
I would prefer this style instead.
void foo()
{
  Option!File worldFile = getAFile("world.json");
  match worldFile {
     Some(value) => {
         auto world          = parseJSON(value);
         Option!File mapFile = getAFile(world["map"]);
         match mapFile {
            Some(mapf) => {
               auto map = parseJSON(mapf);
               //Do something here.
            },
None => enforce(false, "Failed to load: " ~ world["map"]);
         }
     },
     None => enforce(false, "Failed to load: world.json");
   }
}

This can arguably already be done cleaner in D.

if (auto worldFile = getAFile("world.json"))
{
    auto world = parseJSON(worldFile);
    if (auto mapFile = getAFile(world["map"))
    {
        //...
    }
    else enforce(false, "Failed to load: " ~ world["map"]);
}
else enforce(false, "Failed to load: world.json");

Or even:

auto worldFile = enforce(getAFile("world.json"), "Failed to load: world.json");
auto world = parseJSON(worldFile);
auto mapFile = enforce(getAFile(world["map"]), "Failed to load: " ~ world["map"]);
//...

From what I've seen in the Rust community, they try to avoid using match as it's very syntactically heavy. They have all kinds of idioms to avoid doing matches on Option, such as the try! macro, unwrap_or, unwrap_or_else, etc.

That being said, pattern matching has been one of my most-wanted D features for years.

Reply via email to