diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2023-10-01 20:02:22 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2023-10-01 20:02:22 -0400 |
| commit | 4f9e220638ea62443db25d252c4e12ede2c7f9c5 (patch) | |
| tree | ed40edf989f2907e58e3d5fd0fd0a85cde919386 /const.go | |
| parent | 6e3073f5e835b19b39d0853dece386067218b1ce (diff) | |
| download | pfc-4f9e220638ea62443db25d252c4e12ede2c7f9c5.zip | |
error handling and tidying
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 } |