Add type constraints to classgenerics in methods
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 30/100
Research direction
No implementation files or tests are named. Start by reviewing RBS's existing method generic-constraint handling against the examples in the issue; done means an agreed syntax and support for constraints on class generics in method signatures, with coverage for the Array and Enumerable cases.
Written by the indexing model from the issue text.
Description
RBS currently only lets you add type constraints to generic variables when they're declared. (For class/modules, it looks like class Foo[P < _ToS] and for methods it looks like def print: [T < _ToS] (T) -> T).
I propose expanding the method form of this to support arbitrary constraints of class generics:
class CardboardBox[Contents]
def add: (Contents) -> void
def submerge!: {Contents < _Waterproof} () -> void
end
There's a lot of motivating examples of this in the stdlib. Here's a select few:
class Array[E]
# `Array#join` requires the `E` to respond to `to_s`
def join: {E < _ToS} (?string separator) -> String
# `Array#sum` without a default argument requires `E`
# to coerce with integers
def sum: [R < _Add] {E < _Coerce[Integer, R]} () -> R
| [I < _Add[E, O], O < _Add] (I default) -> O
end
module Enumerable[E] : _Each[E]
# `Enumerable#include?` goes `element == object`,
# which requires that `E` implements `==`, not `T`.
def include?: [T] {E < _Equals[T]} (T object) -> bool
end
Few things to note:
- I'm not wed to the syntax—in fact, I'd probably prefer it if it were a part of the
[]expression (egdef include?: [T, E < _Equals[T]]). But that might be difficult to implement, and the braces are currently unused before arguments - This proposal only talks class generics, as that's a somewhat pressing need. However, it might be nice (and if it's easy to do), extend this syntax to support any form of constraints
- Dominant language
- Ruby
- Stars
- 2.2k
- Forks
- 263
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 64
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ruby/rbs
-
InlineParser: `@rbs @ivar` annotation is reported as unused unless it is the first thing in the class bodyPossibly taken @ksss claimed this 50 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 15/100
Maintainers usually reply within 1 day
Similar issues
-
bug f:request-creation professional
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
mysociety/alaveteli#9733 · 1 comment ·
Maintainers usually reply within 1 day
-
needs verification
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
Maintainers usually reply within 13 days
-
🐛 bug 👩💻 équipe CE 🧑🏭 entreprises
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
betagouv/conseillers-entreprises#4808 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100