Result Types and void usage

Jerry via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 15 04:35:54 PDT 2016


On Friday, 15 July 2016 at 08:11:13 UTC, nik wrote:
> //unittest
> //{
> //	auto result_1 = Result!(void, string)(void);
> //	auto result_2 = Result!(void, string)(void);
> //	assert(result_1.is_result);
> //	assert(result_1 == result_2);
> //}

You wanted to handle the void case?

Because there are no void type you have to check the type at 
compile time.
Enter static if...

static if(!is(T == void)) {
         T result() const pure nothrow @safe @property
	{
		return _result;
	}
}

static if(!is(T == void))
         T _result;

static if(!is(T == void)) {
         this(inout T result) inout
	{
		_result = result;
		_is_result = true;
	}
} else {
         this() {
                 _is_result = true;
                  //Or whatever semantics Rust use for void result
         }
}


And about conditional compilation (static if, version etc...):
https://dlang.org/spec/version.html



More information about the Digitalmars-d-learn mailing list