Order-Dependent Lists
Reference: https://rubydoc.info/gems/yard/file/docs/Tags.md#order-dependent-lists
# @return [Array<(String, Integer, Hash)>]
def example; end
result = example
lsp-describe-thing-at-point under example gives:
Returns:
* [Array\<(String, Integer, Hash)\>]
lsp-describe-thing-at-point under result gives:
Array\<String, Integer, Hash\>
Which lead to:
# @return [Array<(String, Integer, Hash)>]
def example; end
body, status, headers = example
unable to be described.
And from: https://yardoc.org/types
It described that:
An fixed-size array of specific objects
Array(String, Symbol)
Results:
an Array containing (a String followed by a Symbol)
Order-dependent lists have partial support at the moment. The Array(Type,...) syntax works, but the parameters are largely ignored in mapping and inference. Improved support is on the roadmap.
Is there any good manual workaround for manually annotating such constructs? Here are some patterns I've tried. All but variant 5 fail (I know that I misuse @type in Variant 1).
Funnily enough, Variant 5 gives me a typeerror within the editor "Declared type Integer does not match inferred type String for variable num5" + similar one for hash5; but this does not happen upon explicit runs of "solargraph typecheck", so I'm not sure what to make of it - but this effect would probably be one for a standalone bugreport anyways?
# @return [Array<(String, Integer, Hash)>]
def example;
return ["hello", 42, {}]
end
#### Variant 1:
# @type str1 [String]
# @type num1 [Integer]
# @type hash1 [Hash]
str1, num1, hash1 = example
# results in all vars being considered "String"
puts [str1, num1, hash1]
#### Variant 2:
str2, num2, hash2 = example
# @type [String]
# @!parse str2
# @type [Integer]
# @!parse num2
# @type [Hash]
# @!parse hash2
# Results in "No information available"
puts [str2, num2, hash2]
#### Variant 3:
str3, num3, hash3 = example
# @type [String]
# @!parse str3 = ""
# @type [Integer]
# @!parse num3 = 0
# @type [Hash]
# @!parse hash3 = {}
myobj = Object.new # Bonus: myobj is now identified as a Hash
# Results in "No information available"
puts [str3, num3, hash3]
#### Variant 4:
str4, num4, hash4 = example
# @type [String]
str4
# @type [Integer]
num4
# @type [Hash]
hash4
# Results in "No information available"
puts [str4, num4, hash4]
#### Variant 5:
array = example
# @type [String]
str5 = array[0]
# @type [Integer]
num5 = array[1]
# @type [Hash]
hash5 = array[2]
# Results in corret types being displayed on hover but in a typeerror in the lines above
puts [str5, num5, hash5]