Documentation
¶
Overview ¶
Package tribool implements a tri-state boolean where the extra state is indeterminate.
Maybe ¶
A tri-state boolean has the values False, True, and Maybe. Maybe represents a value that is neither true or false but it is inderminate. For example, you don't know if an http POST was successful if the connection is dropped after the request was made but before the response came back. This can be modeled with the indeterminate Maybe value.
Tribool provides tri-state logical operators that act like their boolean counterparts. The logic tables are documented below and on each method. There are also methods for mixed Tribool and bool operations.
Parsing ¶
The tribool package is especially useful for parsing flags that may need a default value. For example:
var x string // from somehere var flag bool = tribool.FromString(x).WithMaybeAsFalse()
Parsing is case insensitive. The following table shows what will be parsed to true and false values, anything else (including the empty string) results in the indeterminate value.
case insensitive | result
-----------------+-------
t | Yes
y | Yes
1 | Yes
on | Yes
yes | Yes
true | Yes
f | No
n | No
0 | No
no | No
off | No
false | No
<anything else> | Maybe
Truth Tables ¶
Tribool supports the following binary operations:
| and or nand nor xor iff implies
| a ∧ b a ∨ b ¬(a ∧ b) ¬(a ∨ b) a ⊕ b a ⇔ b a ⇒ b
a b | b ∧ a b ∨ a ¬(b ∧ a) ¬(b ∨ a) b ⊕ a b ⇔ a —
----+-----------------------------------------------------------------
N N | N N Y Y N Y Y
N ? | N ? Y ? ? ? Y
N Y | N Y Y N Y N Y
? N | N ? Y ? ? ? ?
? ? | ? ? ? ? ? ? ?
? Y | ? Y ? N ? ? Y
Y N | N Y Y N Y N N
Y ? | ? Y ? N ? ? ?
Y Y | Y Y N N N Y Y
Tribool supports the following unary operations:
a | not upgrade downgrade --+---------------------------- N | Y N N ? | ? Y N Y | N Y Y
Index ¶
- type Tribool
- func (a Tribool) And(b Tribool) Tribool
- func (a Tribool) AndBool(b bool) Tribool
- func (a Tribool) Equiv(b Tribool) Tribool
- func (a Tribool) EquivBool(b bool) Tribool
- func (a Tribool) Imply(b Tribool) Tribool
- func (a Tribool) ImplyBool(b bool) Tribool
- func (a Tribool) MarshalJSON() ([]byte, error)
- func (a Tribool) Nand(b Tribool) Tribool
- func (a Tribool) NandBool(b bool) Tribool
- func (a Tribool) Nor(b Tribool) Tribool
- func (a Tribool) NorBool(b bool) Tribool
- func (a Tribool) Not() Tribool
- func (a Tribool) Or(b Tribool) Tribool
- func (a Tribool) OrBool(b bool) Tribool
- func (a Tribool) String() string
- func (a *Tribool) UnmarshalJSON(data []byte) error
- func (a Tribool) WithMaybeAsFalse() bool
- func (a Tribool) WithMaybeAsTrue() bool
- func (a Tribool) Xor(b Tribool) Tribool
- func (a Tribool) XorBool(b bool) Tribool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Tribool ¶
type Tribool int
Tribool is a tri-state boolean where the extra state is indeterminate.
The default value for a Tribool is False, just like a boolean.
Example ¶
var u User // initialized elsewhere // The user may not have given their age. canVote1 := u.isOlderThan(18). OrBool(u.isDev()). AndBool(!u.isContestJudge()). WithMaybeAsFalse() canVote2 := (u.isOlderThan(18).WithMaybeAsFalse() || u.isDev()) && !u.isContestJudge() fmt.Println(canVote1, canVote2)
Example (Other) ¶
var n Node // initialized elsewhere // Don't know if a node is activated or not ok1 := Maybe. And(n.IsActive.ImplyBool(n.ID >= 0)). And(n.IsActive.Not().ImplyBool(n.ID == -1)). WithMaybeAsTrue() ok2 := Maybe. Or(n.IsActive.AndBool(n.ID >= 0)). Or(n.IsActive.Not().AndBool(n.ID == -1)). WithMaybeAsTrue() fmt.Println(ok1, ok2)
Example (Parsing) ¶
fmt.Println(
FromString("true"),
FromString("false"),
FromString("yes"),
FromString("no"),
FromString("on"),
FromString("off"),
FromString("1"),
FromString("0"),
FromString("y"),
FromString("n"),
FromString(""),
FromString("huh?"),
)
fmt.Println(
FromString("true").WithMaybeAsTrue(),
FromString("false").WithMaybeAsTrue(),
FromString("").WithMaybeAsTrue(),
)
Output: yes no yes no yes no yes no yes no maybe maybe true false true
const ( // No is equivalent to boolean false No Tribool = no // False is equivalent to boolean false False // Off is equivalent to boolean false Off )
const ( // Maybe represents a value either true or false, but don't know which. Maybe Tribool = maybe // Perhaps is a synonym for Maybe Perhaps // Indeterminate is a synonym for Maybe Indeterminate )
const ( // Yes is equivalent to boolean true Yes Tribool = yes // True is equivalent to boolean true True // On is equivalent to boolean true On )
func FromString ¶
FromString converts a string to a Tribool.
case insensitive | result
-----------------+-------
t | Yes
y | Yes
1 | Yes
on | Yes
yes | Yes
true | Yes
f | No
n | No
0 | No
no | No
off | No
false | No
<anything else> | Maybe
func (Tribool) And ¶
And implements logical and.
| a.And(b) a b | b.And(a) ----+--------- N N | N N ? | N N Y | N ? N | N ? ? | ? ? Y | ? Y N | N Y ? | ? Y Y | Y
func (Tribool) Equiv ¶
Equiv implements logical equivalence.
| a.Equiv(b) a b | b.Equiv(a) ----+------ N N | Y N ? | ? N Y | N ? N | ? ? ? | ? ? Y | ? Y N | N Y ? | ? Y Y | Y
func (Tribool) Imply ¶
Imply implements logical implication.
Implication is not reflexive. That is a.Imply(b) is not the same as b.Imply(a)
a b | a.Imply(b) ----+------------ N N | Y N ? | Y N Y | Y ? N | ? ? ? | ? ? Y | Y Y N | N Y ? | ? Y Y | Y
func (Tribool) MarshalJSON ¶
MarshalJSON marshals tribools to strings, using the Tribool.String() method.
func (Tribool) Nand ¶
Nand implements logical nand.
| a.Nand(b) a b | b.Nand(a) ----+---------- N N | Y N ? | Y N Y | Y ? N | Y ? ? | ? ? Y | ? Y N | Y Y ? | ? Y Y | N
func (Tribool) Nor ¶
Nor implements logical nor.
| a.Nor(b) a b | b.Nor(a) ----+------------ N N | Y N ? | ? N Y | N ? N | ? ? ? | ? ? Y | N Y N | N Y ? | N Y Y | N
func (Tribool) Or ¶
Or implements logical inclusive-or.
| a.Or(b) a b | b.Or(a) ----+---------- N N | N N ? | ? N Y | Y ? N | ? ? ? | ? ? Y | Y Y N | Y Y ? | Y Y Y | Y
func (*Tribool) UnmarshalJSON ¶
UnmarshalJSON supports unmarshalling from a json string (using `FromString()`), a json boolean (using `FromBool()`), and treats anything else as `maybe`.
func (Tribool) WithMaybeAsFalse ¶
WithMaybeAsFalse converts the Tribool to a boolean by coercing Maybe to false.
a | a.WithMaybeAsFalse() --+---------------------- N | N ? | N Y | Y
func (Tribool) WithMaybeAsTrue ¶
WithMaybeAsTrue converts the Tribool to a boolean by coercing Maybe to true.
a | a.WithMaybeAsTrue() --+---------------------- N | N ? | Y Y | Y