rbi icon indicating copy to clipboard operation
rbi copied to clipboard

Make the add_sig block nilable

Open matthewarkin opened this issue 1 year ago • 2 comments

Sig.new defines the block to be nilable and there are plenty of cases where you can add a sig without needing/wanting to use the block syntax.

matthewarkin avatar Oct 08 '24 17:10 matthewarkin

What's the use-case where you would not need to supply the block?

paracycle avatar Oct 08 '24 17:10 paracycle

I'm using tapioca to generate a method with multiple signatures, I have one signature that takes a block and is a void otherwise it returns an enumerator

https://github.com/Shopify/tapioca/blob/3948457fe17ada17ee21a55987dd9d0499860e24/lib/tapioca/rbi_ext/model.rb#L90

Since tapioca doesn't return the method here, the only way I can access it to add the sigs is to use the block on RBI::Method.new (though arguably, tapioca should allow me to pass sigs to create_method or return the method so I can then do method.sigs <<

within the block, I would like to just do:

parameters = [
  RBI::SigParam.new("start", "T.untyped"),
  RBI::SigParam.new("finish", "T.untyped"),
]
create_method("method_name) do |method|
  method.add_sig(
    params: parameters + [RBI::SigParam.new("block", "T.proc.params(object: T::Array[T.untyped]).void")],
    return_type: "void",
  )
  method.add_sig(
    params: parameters,
    return_type: "T::Enumerator[T::Enumerator[T.untyped]]",
  )
end

but since the block is required I can't do that, I gotta do:

method.add_sig(
  params: parameters + [RBI::SigParam.new("block", "T.proc.params(object: T::Array[T.untyped]).void")],
  return_type: "void",
) do |sig|
end

Though of course, I've now realized that I can do:

parameters = [
  RBI::SigParam.new("start", "T.untyped"),
  RBI::SigParam.new("finish", "T.untyped"),
]
create_method("method_name) do |method|
  method.sigs << RBI::Sig.new(
    params: parameters + [RBI::SigParam.new("block", "T.proc.params(object: T::Array[T.untyped]).void")],
    return_type: "void",
  )
  method.sigs << RBI::Sig.new(
    params: parameters,
    return_type: "T::Enumerator[T::Enumerator[T.untyped]]",
  )
end

which gets me what I want, but I don't see why the block should be required on the add_sig call

matthewarkin avatar Oct 08 '24 17:10 matthewarkin