On Sunday, 29 October 2017 at 20:37:21 UTC, bauss wrote:
On Sunday, 29 October 2017 at 20:15:41 UTC, Ola Fosheim Grøstad wrote:
[...]

But casting to bool is what you use to tell whether something is valid or not.

true = valid, false = invalid.

If you want 0 to be valid for a type then you wrap around it with opCast.

Ex.

---
import std.stdio;

struct MyInt
{
        int value;
        
        bool opCast(T : bool)()
        {
                return value >= 0;
        }
}

void main()
{
        MyInt a = MyInt(1);
        MyInt b = MyInt(0);
        MyInt c = MyInt(-1);
        
        if (a) writeln("a is valid");
        if (b) writeln("b is valid");
        if (c) writeln("c is valid");
}
---

Output:
a is valid
b is valid

TL;DR

This could be done by maybe monad.

int? a = 0;
if (a) writeln("a is valid");



BTW: Thanks for implementing the Elvis feature.

Reply via email to