3 ms·
The source code for this is a really nice read, I just read the whole cmd/micro/micro.go like some prose. But one thing jumped at me: if err != nil && !stri
by gkya 9y ago
The source code for this is a really nice read, I just read the whole cmd/micro/micro.go like some prose. But one thing jumped at me:
if err != nil && !strings.HasPrefix(err.Error(), "function does not exist")
I wonder why most modern languages don't provide symbols a la Lisp or keywords a la Ruby, which would allow the above comparison to be a pointer comparison instead of a stribg one.
Edit: add the missing "don't" before "provide symbols".
- chrisseaton 9y agoHow could it be a pointer comparison? It's checking that the string has a prefix, not that the strings are equal.
- throwanem 9y agoThe prefix check in this case is used as a means of typing the error, which many Lisps do via symbols.
- codebeaker 9y agoA string check for an error type in a typed language is a shortcut/laziness really. There's more complex ways (which are less elegant) to typecast the error type into a "concrete" type and figure out if it's really the issue you are looking for. In Go error is an interface type that is expected to implement the `Error() string {}` method. That makes it quite trivial to implement a ErrFunctionDoesNotExist type which wraps the native "error" types returned by the lower down functions, and compare against it with a type assertion. Doing this a lot gets really old, however so maybe the author took a shortcut. If that's the case, at least it's greppable. For what it's worth there's a technique in use throughout the Go stdlib which embues the `error` type with extra methods which allows you to check the specific fault with a url.Parse9), for example https://golang.org/pkg/net/url/#Error https://golang.org/pkg/net/url/#Error
- kazinator 9y ago> I wonder why most modern languages don't provide symbols a la Lisp. Because their implementors don't know WTF that is.