-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathconstraint.go
More file actions
29 lines (23 loc) · 741 Bytes
/
constraint.go
File metadata and controls
29 lines (23 loc) · 741 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package typewriter
import "fmt"
// Constraint describes type requirements.
type Constraint struct {
// A numeric type is one that supports arithmetic operations.
Numeric bool
// A comparable type is one that supports the == operator. Map keys must be comparable, for example.
Comparable bool
// An ordered type is one where greater-than and less-than are supported
Ordered bool
}
func (c Constraint) TryType(t Type) error {
if c.Comparable && !t.comparable {
return fmt.Errorf("%s must be comparable (i.e. support == and !=)", t)
}
if c.Numeric && !t.numeric {
return fmt.Errorf("%s must be numeric", t)
}
if c.Ordered && !t.ordered {
return fmt.Errorf("%s must be ordered (i.e. support > and <)", t)
}
return nil
}