Code Monkey home page Code Monkey logo

Comments (5)

ghiculescu avatar ghiculescu commented on August 30, 2024 1

If so, happy to submit a PR!

Yes please!

from sorbet-rails.

mattxwang avatar mattxwang commented on August 30, 2024

Just curious - is this something we still want to resolve? It seems like ActiveSupport::HashWithIndifferentAccess now exists in sorbet-typed.

If so, happy to submit a PR!

from sorbet-rails.

mattxwang avatar mattxwang commented on August 30, 2024

This is my first time contributing to sorbet-rails, so forgive me for my lack of experience; I have a few questions!

My understanding is that to change the enum values for a model, I should look to change the types in active_record_enum.rb. Is that correct?

And to add - looking at the class def for ActiveSupport::HashWithIndifferentAccess, it subclasses their Hash type, which I'm not sure if it's a template type. Am I understanding how to use this properly?

i.e., should my new code look like this?

return_type = if value_type.length == 1
  "ActiveSupport::HashWithIndifferentAccess[#{value_type.first}]" # should this template type be here?
else
  "ActiveSupport::HashWithIndifferentAccess[T.any(#{value_type.join(', ')})]" # should this template type be here?
end

Or this; but, I feel like I'm losing some type information?

return_type = "ActiveSupport::HashWithIndifferentAccess"

from sorbet-rails.

ghiculescu avatar ghiculescu commented on August 30, 2024

Hmm, this works fine, but as you say it loses type information.

But this does not.

@jeffcarbs you added the ActiveSupport::HashWithIndifferentAccess sig here but do you mind sharing how it's typically used?

from sorbet-rails.

mattxwang avatar mattxwang commented on August 30, 2024

While we wait for @jeffcarbs' answer, I'll note that the documentation for the class has examples where values are of different types:

rgb = ActiveSupport::HashWithIndifferentAccess.new

rgb[:black] = '#000000'
hash = ActiveSupport::HashWithIndifferentAccess.new(a: 1)

So I'm sure that there are typical uses where the values are different types.

This seems suitable for a generic type to me, which may make upgrading for users easier since it then just becomes a find-and-replace for T::Hash[T.any(String, Symbol), -> ActiveSupport::HashWithIndifferentAccess[. In that case, we also won't lose type information!

However, I'm not sure if this is bad practice since we're modifying how ActiveSupport::HashWithIndifferentAccess is defined internally, and that sounds like a breaking change to me. It also seems like generics are still an experimental feature in sorbet. My understanding is that we would lose generic types (type erasure?). It may be difficult to implement (though I'd still like to try if possible!).

Any thoughts @ghiculescu? I'm coming with little experience with sorbet or rails (my background is not in ruby) so any thoughts are appreciated!

from sorbet-rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.