-
Notifications
You must be signed in to change notification settings - Fork 136
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
Refactor DSL compilers #795
Conversation
Refactor some constants related to locating constants and finding constant by name to be under that namespace.
f844129
to
0a044d6
Compare
sig { returns(T::Set[Module]) } | ||
def self.processable_constants | ||
@processable_constants ||= T.let( | ||
Set.new(gather_constants).tap(&:compare_by_identity), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need the tap
here? Wouldn't it be enough to invoke compare_by_identity
straight away?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think when I originally wrote this, I thought that compare_by_identity
didn't return self
, so that's why. I can update this.
|
||
include Reflection | ||
extend Reflection |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it intentional that we're including and extending the same module? Is there any way we could consolidate these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is intentional, since we want gather_constants
methods to have access to Reflection
methods too, but that method is on the singleton class now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if there is a better way to do this, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I guess it'll be hard to escape this. Just checking.
It looks like the PR description has a small typo in the "new" code. I think it should be: -def gather_constants
+def self.gather_constants Might be worth updating for posterity since (like I did) some folks may come to this PR to see how to refactor their custom DSL compilers. |
Thanks for pointing this out @jeffcarbs, fixed the code in the description. |
When I did this, I started getting this error in my DSL Generators
If I added Does the code sample need to look like: class Foo < Tapioca::Dsl::Compiler
ConstantType = type_member(upper: Module)
Elem = type_member(fixed: T.class_of(FooDsl))
sig { override.void }
def decorate
end
sig { override.returns(T::Enumerable[Module]) }
def self.gather_constants
end
end ? |
👋 Hey Eric, We renamed I believe this is enough: class Foo < Tapioca::Dsl::Compiler
ConstantType = type_member(fixed: T.class_of(FooDsl))
sig { override.void }
def decorate
end
sig { override.returns(T::Enumerable[Module]) }
def self.gather_constants
end
end Hope this helps. |
I updated the example in the description to reflect that the type member name is |
Motivation
This is a breaking and backwards-incompatible change to how DSL compilers are structured. We are moving from creating a single instance of each compiler for the whole pipeline run to creating a new compiler instance for each constant that is processed. This allows the compiler interface to be cleaner (since there are less things that need to be passed to
decorate
) and the compiler to hold state if it wants. In particular, the fact that each compiler had to passconstant
between its methods was a smell for me.The general interface for a DSL compiler is changing from:
to
Implementation
Compiler
base classDslCompiler
helper to adapt to the new structureTests
No added tests, refactored existing ones.