-
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
Add Type.Addr().Implements() in DSL #137
Closed
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sebastien-rosset
changed the title
Add Pointer() function in DSL
Add Type.Pointer().Implements() in DSL
Dec 16, 2020
…eTo and Type.Pointer().ConvertibleTo
@cristaloleg , @quasilyte , any thoughts about this change? |
sebastien-rosset
changed the title
Add Type.Pointer().Implements() in DSL
Add Type.Addr().Implements() in DSL
Dec 30, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix for #129
Suppose in a particular application the developer must instantiate a
Job
struct using a factory method instead of plain struct initializer. The factory method initializes the fields as needed. If in the future new fields are added, the factory method will take care of doing the initialization in one place.I want to print a warning such as Replace struct initialization with call to factory method NewXyz().
Suppose further there are many generated structs (not just one one Job). All these structs must be instantiated using a factory method. It just so happens that all these structs implements a particular interface.
Consider the following code:
If there is a single
Job
struct, then it's easy to create a match rule usingMatch("worker.Job{$*_}")
. But what if there are hundreds a structs, possibly code-generated, that implements a specific interface?In that case, we can start by using the existing
Implements
keyword in the DSL:However, this does not work because as shown in the code sample,
worker.Job
is not a pointer, thereforeImplements("worker.Runner")
would always return false.The proposal is to add a new
fluent.ExprType.Addr() ExprType
function in the DSL. The function returns a type pointer, therefore anyExprType
function (includingImplements
) can be called on the returnedExprType
value.The new
Addr()
method can be used for any use case where the matching code is not a pointer, but a pointer type is needed to invokeImplements
.I don't have good use cases for
Type.Addr().Is()
,Type.Addr().IsConvertible
andType.Addr().IsAssignable()
so I didn't add these. Seeking feedback before deciding what to do next.