-
Notifications
You must be signed in to change notification settings - Fork 234
Open
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
endFew 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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels