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

Introduce Type builder interface #331

Merged
merged 2 commits into from
Jul 31, 2024
Merged

Introduce Type builder interface #331

merged 2 commits into from
Jul 31, 2024

Conversation

Morriar
Copy link
Collaborator

@Morriar Morriar commented Jul 31, 2024

This interface let the user create RBI types manually:

type = Type.nilable(Type.simple("Foo"))
type.to_s # => T.nilable("Foo")

On top of providing more safety with static typing, the interface will apply some sanity rules to what the user builds to avoid common mistakes when generating RBIs:

type = Type.simple("<invalid>") # => Error: Invalid type name: <invalid>

The interface will also take care of simplifying some unnecessary complex types:

type = Type.any(Type.simple("Foo"), Type.simple("NilClass"))
type.to_s # => T.nilable("Foo")

This PR changes the Model classes to allow passing either Strings or Types to relevant classes such as Sig, SigParam or TStruct.

cc. @paracycle

@Morriar Morriar added the feature New feature label Jul 31, 2024
@Morriar Morriar self-assigned this Jul 31, 2024
@Morriar Morriar requested a review from a team as a code owner July 31, 2024 16:01
@Morriar Morriar requested review from amomchilov and egiurleo July 31, 2024 16:01
@Morriar Morriar force-pushed the at-type-builder branch 2 times, most recently from 049dd0c to 3dac103 Compare July 31, 2024 16:07
This interface is used to manually build RBI types.

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Co-authored-by: Ufuk Kayserilioglu <ufuk.kayserilioglu@shopify.com>
Copy link
Member

@paracycle paracycle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, just a couple of comments.

@@ -586,7 +586,7 @@ def add_block_param(name)
end
def add_sig(
params: [],
return_type: nil,
return_type: "void",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also be Type.void like below?

@@ -839,7 +839,7 @@ def visit_call_node(node)
end
end
when "void"
@current.return_type = nil
@current.return_type = "void"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, maybe this should be Type.void? Or do want to wait for the Type parser to make that change?

Copy link
Collaborator Author

@Morriar Morriar Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I should have changed all of them to "void" for now

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Co-authored-by: Ufuk Kayserilioglu <ufuk.kayserilioglu@shopify.com>
@Morriar Morriar merged commit ec9da9e into main Jul 31, 2024
8 checks passed
@Morriar Morriar deleted the at-type-builder branch July 31, 2024 18:15
@Morriar Morriar mentioned this pull request Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants