From 4f9e220638ea62443db25d252c4e12ede2c7f9c5 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Sun, 1 Oct 2023 20:02:22 -0400 Subject: error handling and tidying --- const.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'const.go') diff --git a/const.go b/const.go index ff0d09c..3808e82 100644 --- a/const.go +++ b/const.go @@ -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 } -- cgit v1.2.3