Skip to content
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

Add in ::Ops interfaces #1638

Closed

Conversation

sampersand
Copy link
Contributor

@sampersand sampersand commented Nov 22, 2023

This adds in the Ops module, which contains definitions for all the overloadable Ruby operators, but three ([], []=, and .() are missing, and'll be added when #1639 gets resolved.)

I think it makes sense to add these, for multiple reasons:

  • All the operators have fixed signatures (these in particular taking one or no additional arguments, no blocks, and always return one type.)
  • There's a limited amount of operators, with no plans to add support for custom operators.
  • Having a centralized place for operator names makes it easy for end-users to reference them without having to create their own
    • prevent proliferation of many slightly-incompatible signatures (eg assuming == always returns bool and not boolish sometimes, etc)
  • While a few of the operators could have homes (<=> in Comparable, ! in BasicObject, etc.) a vast majority of them don't have a clearly defined location (+, <<, =~, just to name a few).
    • Having them all in one location is a lot more consistent, and makes finding signatures a lot easier (what if you didn't know that ! was defined in BasicObject?)

Ruby's intended for programmer happiness after all

@ParadoxV5
Copy link
Contributor

ParadoxV5 commented Nov 22, 2023

a vast majority of them don't have a clearly defined location (+, <<, =~, just to name a few).

Most of which (e.g., not +: it’s used by Enumerable#sum) also don’t have a practical reason to be included, as in, will people actually use those to describe all the duck-typing implementation details as much as possible? It’d be too specific (possibly verbose) to make programmers happy at that point.

@ParadoxV5
Copy link
Contributor

module Ops

There is no module Ops in Ruby. The namespace should be under say RBS instead. Or maybe leave them toplevel like what builtin.rbs does.

@ParadoxV5
Copy link
Contributor

ParadoxV5 commented Nov 22, 2023

Tracker: What methods use which operators

  • +: Enumerable#sum
  • -: Numeric#%
  • *: Numeric#%
  • /: Numeric#%
  • %:
  • **:
  • =~: Object#!~, Regexp#=~
  • < > <= >=:
    Comparable-like but not (didn’t bother to include perhaps?) Comparable (e.g., Hash, Set)
    • I’m positive with grouping them under say interface _ComparableLike though.
  • <<:
  • >>:
  • &:
  • |:
  • ^:
  • ~:
  • -@:
  • +@:
  • []: String#encode fallback kwarg
  • []=:
  • .(): Really just a syntax sugar for .call (see Somehow add metaprogramming to type parameters #1639 point 2)

(what if you didn't know that ! was defined in BasicObject?)

Folks would take the fact that these things are always defined for granted. Thus they’d happily write Object or untyped if not BasicObject or top. (Why BasicObject and top aren’t synonymous is perhaps a different topic for a different thread.)

  • BasicObject
    • ==
    • !=
    • !
  • Object Kernel
    • !~
    • ===
    • <=>: Comparable

@soutaro
Copy link
Member

soutaro commented Nov 28, 2023

@sampersand

Generally, I agree with @ParadoxV5. It's not very clear if we have practical reasons for these operators. We don't need a standard set of those operators technically, because interface types are structural.

A few more thoughts:

  1. Is it better to give more abstract names to the interfaces, like _Plus instead of _Add?
  2. The surrounding module should be under ::RBS module, instead of ::Ops

@sampersand
Copy link
Contributor Author

Let's put this on the backburner for now, closing it until later.

@sampersand sampersand closed this Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants