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

Ease of integration with existing typed codebases #2270

Open
ianks opened this issue Dec 3, 2019 · 3 comments
Open

Ease of integration with existing typed codebases #2270

ianks opened this issue Dec 3, 2019 · 3 comments

Comments

@ianks
Copy link

@ianks ianks commented Dec 3, 2019

Problem

We have a few large codebases which have been "runtime-typed" using the DRY.rb family of gems for a while now. All of the necessary type information used in our apps has been declared already.

For example, we have typed structs with attributes defined like:

class User < Dry::Struct
  attribute :name, Types::Strict::String
  attribute :age,  Types::Strict::Integer
end

Ideally, it would be simple to communicate this type information to sorbet. There are currently two options, both of which are fairly painful:

  1. Convert to using T::Struct. While possible, this is an incredibly tedious solution and goes against the principles of gradual typing, because it would require a lot of rewriting.

  2. Use a plugin. This one is challenging as it operates on the text level of RBI. It requires parsing the source and printing RBI definitions, which is error-prone.

In an ideal world, I would be able to add sorbet to my Gemfile and get static checks for these types out of the box, and rely on dry-types and dry-struct to handle the runtime checks.

Proposed solution

I do not know what the best solution would be, but there are some things we can use to make this much easier.

  1. Use AST information from gems that provide them:
dev(main)> Types::String.to_ast
=> [:nominal, [String, {}]]
  1. Ability to detect classes for custom type generation, i.e. if we encounter something which responds to to_ast, or inherits from a certain class, be able to pass along the type information

Summary

These are just some loose thoughts I've had when adding sorbet to our project. Overall, I think this project has huge potential and am very excited for Ruby's typed future. Thank you!

@ianks

This comment has been minimized.

Copy link
Author

@ianks ianks commented Dec 6, 2019

For reference, I also opened up this issue on the dry-rb forum for further discussion/ ideation.

@ianks

This comment has been minimized.

Copy link
Author

@ianks ianks commented Dec 12, 2019

Is there custom type checking code in the C extension for type checking T::Struct in Sorbet? I'm wondering if it would be possible to enable a mechanism for directly plugging into the compiler for custom checking. This could be a lot easier than outputting text. Thoughts?

@ianks

This comment has been minimized.

Copy link
Author

@ianks ianks commented Dec 12, 2019

Perusing the source code, DSLBuilder looks like it could accommodate this type of use case. @ptarjan / @pt-stripe Could DSLBuilder be used to plug into something like Dry::Types?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.