-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify "can call X method" use case #331
Comments
quasilyte
added a commit
that referenced
this issue
Jan 6, 2022
HasMethod reports whether a type has a given method. Unlike Implements(), it will work for both value and pointer types. fn argument is a function signature, like `WriteString(string) (int, error)`. It can also be in form of a method reference for importable types: `io.StringWriter.WriteString`. To avoid confusion with Implements() method, here is a hint when to use which: - If you want to check if it's possible to call F on x, use HasMethod(). - If you want to know if you can pass x as I interface, use Implements(). Refs #331
quasilyte
added a commit
that referenced
this issue
Jan 6, 2022
HasMethod reports whether a type has a given method. Unlike Implements(), it will work for both value and pointer types. fn argument is a function signature, like `WriteString(string) (int, error)`. It can also be in form of a method reference for importable types: `io.StringWriter.WriteString`. To avoid confusion with Implements() method, here is a hint when to use which: - To check if it's possible to call F on x, use HasMethod(F) - To check if x can be passed as I interface, use Implements(I) Refs #331
quasilyte
added a commit
that referenced
this issue
Jan 6, 2022
HasMethod reports whether a type has a given method. Unlike Implements(), it will work for both value and pointer types. fn argument is a function signature, like `WriteString(string) (int, error)`. It can also be in form of a method reference for importable types: `io.StringWriter.WriteString`. To avoid confusion with Implements() method, here is a hint when to use which: - To check if it's possible to call F on x, use HasMethod(F) - To check if x can be passed as I interface, use Implements(I) Refs #331
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Implements()
would miss a few cases that can be solved by a custom filter, but for the simplest cases like below we should be able to use a simple builtin.Unfortunately, this will not work for values that have a type of
T
, but implement interface only by pointer (so, only*T
values will match here). For this particular case, this code will be checked without warnings (bad):What we want to check here is that
$w.WriteRune
can be rewritten as$w.WriteByte
.Maybe this API can be OK:
For simplicity, we can allow 1-method interfaces as arguments too:
This new method should be at least as fast as the current approach (or even faster).
The text was updated successfully, but these errors were encountered: