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

_Callable interface in builtin #420

Open
HoneyryderChuck opened this issue Oct 16, 2020 · 5 comments
Open

_Callable interface in builtin #420

HoneyryderChuck opened this issue Oct 16, 2020 · 5 comments

Comments

@HoneyryderChuck
Copy link
Contributor

I was looking at https://github.com/ruby/rbs/blob/c64d1f5ba5a4fbc95ffb1d72d783ee39b17d60fe/stdlib/builtin/builtin.rbs, where a lot of common knownn interfaces are stored. I was wondering whether there should be one for "callable" objects, that is: implements #call. Objects implementing this interface can already be passed to places where are expected, for example.

interface _Callable
  def call: (*untyped) -> void
  alias [] call
end
@marcandre
Copy link
Member

Maybe, but very few builtin methods take such a parameter. I can think of Enumerable#find (which is currently badly defined)

Note that the return type should not be void but top. Also the interface should not depend on []:

interface _Call
  def call: (*top) -> top
end

Still, it might be best not to, since the above interface is too lax, usually you will want to specify the type of inputs the call method should be expecting.

@HoneyryderChuck
Copy link
Contributor Author

In the benefit of understanding, what does "top" stand for? I struggled a bit with the meaning of it.

Perhaps you're right, but a Callable with a T reference seems more suitable to my example. All things considered I might backtrack a bit on this until I come up with compelling use cases.

@soutaro
Copy link
Member

soutaro commented Oct 17, 2020

If you have a specific method in your mind, like Enumerable#find, defining an interface would be better.

interface _NotFound[A]
  def call: () -> A
end

module Enumerable[X, Y]
  def find: [T] (_NotFound[T]) -> (X | T)
  ...
end

@HoneyryderChuck
Copy link
Contributor Author

I had this in mind: https://docs.python.org/3/library/typing.html#callable

@HoneyryderChuck
Copy link
Contributor Author

After thinking more about this, I think that I do not necessarily want a Callable interface, rather an "inline" way of saying "something implementing #call", i.e. I think that rbs could have syntactic sugar for defining these 1 method-interfaces.

  def callables: () -> Array[Imp[call:(*untyped) -> top]]

How that would look like, I have no idea :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants