0

Is there a way to avoid creating temporary variables just to document block's argument YARD type?

The snippet below works but these temporary variables doesn't look good.

foos.each_with_object(Set.new) do |foo, obj|
  # @type [Set]
  set = obj
  # now set/memo variable is semi-typed and LSP can do type-based autocompletion
end
BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
  • If I recall correctly, these should be documented in `#each_with_object` using `@yieldparam` and `@yieldreturn` from Yard – Frederik Spang Jun 20 '22 at 19:35
  • @FrederikSpang is correct. [See Here](https://rubydoc.info/gems/yard/file/docs/Tags.md#yield) for the docs and basic examples of the `@yield`, `@yieldparam`, and `@yieldreturn` tags – engineersmnky Jun 27 '22 at 20:46

1 Answers1

0

Use @param like so:

# @param foo [Integer]
# @param set [Set]
foos.each_with_object(Set.new) do |foo, set|
  # foo & set are LSP/YARD typed in this block
end

YARD Solargraph @param type

I'm not sure if it is fully idiomatic YARD approach but it works in LSP/Solargraph.

Originally I found this example in the Solargraph spec.

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91