diff options
Diffstat (limited to 'const.go')
| -rw-r--r-- | const.go | 20 |
1 files changed, 12 insertions, 8 deletions
@@ -2,16 +2,20 @@ package main import "math" -// parseConstant returns nil if s is not a valid constant. -func parseConstant(s string) *float64 { +func parseConstant(s string) (float64, error) { switch s { case "pi": - // Assign to variable because can't take address of constant. - var pi float64 = math.Pi - return &pi + return math.Pi, nil case "e": - var e float64 = math.E - return &e + return math.E, nil } - return nil + return 0, InvalidConstantErr{s} +} + +type InvalidConstantErr struct { + s string +} + +func (e InvalidConstantErr) Error() string { + return "invalid constant: " + e.s } |