Skip to content
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

Closed
quasilyte opened this issue Dec 30, 2021 · 0 comments
Closed

Simplify "can call X method" use case #331

quasilyte opened this issue Dec 30, 2021 · 0 comments

Comments

@quasilyte
Copy link
Owner

quasilyte commented Dec 30, 2021

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.

func writeByte(m dsl.Matcher) {
	// utf8.RuneSelf:
	// characters below RuneSelf are represented as themselves in a single byte.
	const runeSelf = 0x80
	m.Match(`$w.WriteRune($c)`).
		Where(m["w"].Type.Implements("io.ByteWriter") && (m["c"].Const && m["c"].Value.Int() < runeSelf)).
		Suggest(`$w.WriteByte($c)`)
}

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):

var buf bytes.Buffer
buf.WriteRune('\n')

What we want to check here is that $w.WriteRune can be rewritten as $w.WriteByte.

Maybe this API can be OK:

Where(m["w"].Type.HasMethod("WriteByte(byte) error"))

For simplicity, we can allow 1-method interfaces as arguments too:

Where(m["w"].Type.HasMethod(`io.ByteWriter`))

This new method should be at least as fast as the current approach (or even faster).

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant