On Tuesday, 27 October 2015 at 17:48:04 UTC, Meta wrote:
On Tuesday, 27 October 2015 at 15:06:07 UTC, TheFlyingFiddle wrote:
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.

Yes this is much cleaner. But it does not really force a user to consider the empty case.

I mean this would still compile.
auto worldFile = getAFile("world.json");
auto world     = parseJSON(worldFile);
auto mapFile   = getAFile(world["map"]);
auto map       = parseJSON(mapFile);

What I was after was a way to at compile time ensure that all accesses to the value in the optional type are considered. From my uses of Maybe in haskell i know this get's annoying quick so I dunno if it's a good thing. But atleast you would know that in all places that optionals are accessed a handler for the empty case would be present.

Reply via email to