Mutation methods type-locking question

Open
#2,819 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
ruby
Domain
tooling

Research direction

Start with the Hash#transform_values! signature described in this issue and compare it with the original transform_values signature from ruby/rbs#728. Reproduce the Net::HTTPResponse example with Steep and check whether the mutation can change Hash[String, Array[String]] to Hash[String, String] without a type error.

Written by the indexing model from the issue text.

Description

We (at Datadog) stumble upon situation when we use Hash#transform_values! and we get into the trap of type-locking introduced in this PR https://github.com/ruby/rbs/pull/728

What do you think of making both mutation methods have the same signature, like this one (taken from the original transform_values)?

  def transform_values: () -> Enumerator[V, Hash[K, untyped]]
                      | [A] () { (V) -> A } -> Hash[K, A]

Because now there is no way to change the type of say, this expression (response is Net::HTTPResponse)

response.to_hash.transform_values! { |value| Array(value).join }

when run Steep it will complain that original Hash typed as Hash[String, Array[String]] and if something expects to get Hash[String, String] the check will fail, but in fact we mutate the value to be a String.

Dominant language
Ruby
Stars
2.2k
Forks
263
Avg merge
1d 20h
Merged PRs (30d)
64

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ruby/rbs

All issues in ruby/rbs

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.