-
Notifications
You must be signed in to change notification settings - Fork 217
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
Suggestion/discussion about typing with the splat operator (*, **, "rest" or "varargs") #322
Comments
Hi @abravalheri, Thank you for reporting this issue. The discussion about Python and Scala really helps me understand the possible solutions. I'm not very open to add a new (complicated) feature to support heterogeneous rest args. However, I'm thinking of adding something to support delegation would be an option. Our understanding currently is that one of the most common use cases which require heterogeneous rest args is delegation: passing all of the arguments to another method. (As you showed us in your Do you think supporting delegation makes sense? |
Hi @soutaro, thank you very much for your feedback. I do think delegation makes a ton of sense and covers a lot of ground. Just a question: does it mean that the signature of the outer method/proc have to be 100% the same of the encapsulated one? Or we would have some flexibility to add some control parameters consumed by the outer method and not forwarded? I don't know if the following example makes sense or is already handled by the current algorithms, but this is my attempt of exemplify my previous paragraph (continuing with my previous example): class Effect
...
def attempt(times, *args) # or with kw: def attempt(*args, times: 3)
count = 0
begin
return @callable.(*args)
rescue => e
count += 1
if count < times
puts "Attempt #{count} failed with exception `#{e.inspect}`, retrying..."
retry
else
sleep 0.1 * count
raise
end
end
end
end |
An update from the Python community: After starting with a very similar approach (of assuming uniform rest/variadic args) and facing the problems listed in the first comment of this thread, they recently accepted a new proposal on how to express heterogeneous rest args, as described in https://www.python.org/dev/peps/pep-0612. Although possibly not implemented yet, it went through the formal process of approval and should arrive in future versions of the language/type checker. |
Another update regarding the last comment. The methodology for argument forwarding/heterogeneous varargs described by PEP612 is now implemented in Python 3.10 (currently in beta): https://docs.python.org/3.10/library/typing.html#callable |
Are there any updates on this? Would love to see this functionality in RBS |
Disclaimer: I'm approaching this super naively. Let's say you have:
Then So for RBS to match the signature of But it occurred to me that is may difficult, if at all possible, statically right away. Consider:
Imagine we have per element typing then type would change In that case, currently
What may help though is taking inspiration from doing the checks dynamically, like it is done for variable type checks:
So one could (theoretically, Steep does not support that) do:
And from that it would presumably be able to infer that And indeed it does when using intermediate variables:
I just wish Steep would allow eschewing the intermediate variables, especially with Note that this is only possible because args is known not to be mutated concurrently, e.g this would cause problems:
Anyway, that would presumably not help when such checks can't be done, like when contrary to So, back to the original example, with the knowledge that heterogeneous splat args are actually not heterogeneous but a uniform union type, I changed it this way:
Now, there are whisker casts in there, isolating specific issues in local variables:
Anyway, this fails with:
Instead, by typing
Then Changing the call to have a non Numeric argument:
Produces the expected error:
To enforce the type of the first argument, we'd be required to actually enforce it on the callable:
Which produces:
As mentioned, it odoes allow one to pass this though:
But at least we're halfway there and check for unexpected types. Tangent: back to this:
I've been toying with generics for this:
Which, if you are in a context where at somepoint you can specify the
Could be useful to handle the return type as well in a mostly generic fashion and get rid of the last type annotation on callable. |
Discussion about preferring to tackle that as delegation notwithstanding, regarding the syntax of the proposed change:
This does not seem to tackle enforcing position (or key for I think I would prefer mimicking records and tuples, which seem to map better with
In addition it seems to be better at enforcing:
Which tuples and records seem to naturally achieve. This would probably not cover variadic arguments though. |
Hi @lloeki, using records and tuples do sound like an improvement over the original proposal. I think, however, they are not incompatible. In my original post, I added a table (slightly modified version):
In the second row, we can assume that In the conceptual level, nowadays all variadic/keyword arguments are assumed to be from the same time. With the change, the type itself would be "splattered", which allows for records and tuples. |
Hello. Lately I have been playing with the type system in Ruby and I notice some patterns that cannot be represented by the RBS syntax... So I wanted to start a discussion about it. I hope we can get some fruitful insights.
(OBS.: The discussion here is based on the example about
splatting
in the syntax document and one of my doubts in the issue in soutaro/steep#160.)Motivation
In the syntax doc, it seems that the type of rest arguments can only be uniform (i.e. the same type for all the entries)...
So in the following example it seems that the current grammar does not allow writing
<something>
in such a way that the arguments given tolazy
are type matched against the arguments in the signature or@callable
(please correct me if I am wrong)Proposal
Based on this my proposal would be change the meaning of the type associated with the splat operator to refer to the entire array of arguments instead of each argument individually (i.e. make
T
in(*T) -> S
mean the type of the array instead of the type of each individual element).Please notice that with this change we can represent both uniform and heterogeneous lists of arguments (if someone wants to enforce uniform types, it can be done with
(*Array[T]) -> S
). Without the grammar is somehow incompatible with the main use of the splat operator nowadays (heterogeneous types).Please notice the suggestion is also valid for keyword arguments:
T
in(**T) -> S
would represent the entire type of the hash of keywords, not the type of each keyword value. If someone wants to enforce uniformity in the type, that could be done with `(**Hash[Symbol, T]) -> S.Summary of the proposal:
(*A, **B) -> C
(*Array[A], **Hash[Symbol, B]) -> C
(*A, **B) -> C
Other implementations/languages
By the documentation, Sorbet doesn't seem to support heterogeneous rest args either, and apparently this decision was inspired by Scala. However Ruby syntax is different from Scala in that respect (disclaimer: I might be wrong here, I am not a Scala programmer). Apparently, according to this website, Scala prohibits the
un-splatting
of heterogeneous lists, and instead provides an alternative method to obtain virtually the same effect. This is not the case of Ruby...un-splatting
of heterogeneous arrays is super-common (and the standard way we forward args between method calls), and I don't remember now any existing alternative for un-splatting...Therefore, I believe taking inspiration from Scala is not a good call on this matter.
We can instead look on how our friends in the Python community are dealing with this, since the grammar for splats in Python and Ruby is almost identical. Indeed they also opted by using uniform rest args, however this decision created a series of issues, and difficulties to produce type signatures even inside the standard library, that they are currently struggling to solve. Particularly it is impossible to annotate complex decorators - one of the pillars of Python's high-order functions - that modify/hide/augment the argument list of the decorated functions -- please notice that the decorator pattern in Python is similar to the pattern of wrapping blocks in Ruby/passing blocks around. The following links represent some of the issues pointed out by the Python community and the attempts to solve the limitation (mostly based on providing an alternative construct interpreted by the type checker).
It seems that main contributors of Python's reference type checker do appreciate that heterogeneous rest args are important and are debating for an alternative instrument to represent it, other than directly in the type annotation grammar to avoid breaking retro-compatibility. However, since the type grammar for stubs is Ruby is not consolidated yet, I see this as a great opportunity, so we can adopt a grammar that can handle both use cases (heterogeneous and uniform) and be more future proof.
The text was updated successfully, but these errors were encountered: