views:

170

answers:

3

I'm trying to implement a Tribool type using http://www.boost.org/doc/libs/1_41_0/doc/html/tribool.html as reference.

I'm using a struct since it's a primitive type and shouldn't be extendable. I know there are 3 types of Tribools---True, False, and Unknown, and the default constructor should provide a False Tribool. My question is, what data type do I set True, False and Unknown to? Right now I have:

struct Tribool
{
    //True, False, and Unknown public constants
    public static readonly Tribool TRUE, FALSE, UNKNOWN;

    //Constructors
    public Tribool()
    {
        Tribool = FALSE; //use ValueType instead?
    }

but I'm not sure if that's correct, since it looks like I'm just setting a Tribool to another Tribool. Should I use ValueType instead? It popped up when I was typing in VS, and it sounds sensible, but I wasn't able to find a lot of information on it from Google.

Also, the Tribool needs to be able to operate with regular bools, which means "true" and "false" need to be overloaded. Would this require an operator overload? Or should it just be a method that returns a bool?

Thanks in advance!

Edit: Sorry, I should have mentioned this was for an assignment. So I can't use bools even though it's a lot more practical as many of you have pointed out.

+6  A: 

Why not just use bool? which is a nullable boolean?

Brian Rasmussen
+4  A: 
bool?

done. That do? In particular, the C# compiler will provide the "lifted" operators to map to bool for you. Arguably, though, it might be slightly larger than just a bool or single enum.

Note: don't use ValueType, as this would actually be a boxing operation.

If you couldn't use bool? (i.e. you wanted to implement from scratch) I would map it to an enum (possibly a byte enum, but I'd default to int as normal):

public struct Tribool : IEquatable<Tribool> {
    public static Tribool True { get { return new Tribool(true); } }
    public static Tribool False { get { return new Tribool(false); } }
    public static Tribool Unknown { get { return new Tribool(); } }
    enum TriboolState { Unknown = 0, True = 1, False = 2 }

    private readonly TriboolState state;
    public Tribool(bool state) {
        this.state = state ? TriboolState.True : TriboolState.False;
    }
    // default struct ctor handles unknown
    public static bool operator true(Tribool value) {
        return value.state == TriboolState.True;
    }
    public static bool operator false(Tribool value) {
        return value.state == TriboolState.False;
    }
    public static bool operator ==(Tribool x, Tribool y) {
        return x.state == y.state;
    }
    public static bool operator !=(Tribool x, Tribool y) {
        return x.state != y.state; // note: which "unknown" logic do you want?
                                   // i.e. does unknown == unknown?
    }
    public override string ToString() {
        return state.ToString();
    }
    public override bool Equals(object obj) {
        return (obj != null && obj is Tribool) ? Equals((Tribool)obj) : false;
    }
    public bool Equals(Tribool value) {
        return value == this;
    }
    public override int GetHashCode() {
        return state.GetHashCode();
    }
    public static implicit operator Tribool(bool value) {
        return new Tribool(value);
    }
    public static explicit operator bool(Tribool value) {
        switch (value.state) {
            case TriboolState.True: return true;
            case TriboolState.False: return false;
            default: throw new InvalidCastException();
        }
    }
}
Marc Gravell
+2  A: 

What's wrong with Nullable<bool>?

rpetrich