-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathobject_float.go
68 lines (57 loc) · 1.65 KB
/
object_float.go
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package object
import (
"fmt"
"hash/fnv"
"strconv"
)
// Float wraps float64 and implements the Object interface.
type Float struct {
// Value holds the float-value this object wraps.
Value float64
}
// Inspect returns a string-representation of the given object.
func (f *Float) Inspect() string {
return strconv.FormatFloat(f.Value, 'f', -1, 64)
}
// Type returns the type of this object.
func (f *Float) Type() Type {
return FLOAT
}
// True returns whether this object wraps a true-like value.
//
// Used when this object is the conditional in a comparison, etc.
func (f *Float) True() bool {
return (f.Value > 0)
}
// ToInterface converts this object to a go-interface, which will allow
// it to be used naturally in our sprintf/printf primitives.
//
// It might also be helpful for embedded users.
func (f *Float) ToInterface() interface{} {
return f.Value
}
// Increase implements the Increment interface, and allows the postfix
// "++" operator to be applied to float-objects
func (f *Float) Increase() {
f.Value++
}
// Decrease implements the Decrement interface, and allows the postfix
// "--" operator to be applied to float-objects
func (f *Float) Decrease() {
f.Value--
}
// HashKey returns a hash key for the given object.
func (f *Float) HashKey() HashKey {
h := fnv.New64a()
h.Write([]byte(f.Inspect()))
return HashKey{Type: f.Type(), Value: h.Sum64()}
}
// JSON converts this object to a JSON string.
func (f *Float) JSON() (string, error) {
return fmt.Sprintf("%f", f.Value), nil
}
// Ensure this object implements the expected interfaces.
var _ Decrement = &Float{}
var _ Hashable = &Float{}
var _ Increment = &Float{}
var _ JSONAble = &Float{}