class YARD::Tags::TypesExplainer::CollectionType
@private
‘<…>`’s type parameters are conventionally used two ways - as a homogeneous collection’s implicit union of element type(s), or as a class’s distinct, positional type parameters, e.g. ‘Result<Success, Failure>` - and this class renders either way, chosen per instance by {#union?}. This keeps that choice entirely out of the parser: every `<…>` becomes one of these, regardless of name.
Constants
- UNION_COLLECTION_NAMES
-
Typenames known in advance to be genuinely homogeneous collections, where ‘<…>`’s type parameters really do mean “any one of these”. Fixed and not user-configurable -YARDhas no syntax for a class to declare its own ‘<…>` convention, so this can only ever be a hardcoded, conservative list.
Attributes
Public Class Methods
Source
# File lib/yard/tags/types_explainer.rb, line 144 def initialize(name, types) @name = name @types = types end
Public Instance Methods
Source
# File lib/yard/tags/types_explainer.rb, line 149 def to_s(_singular = true) if union? "#{indefinite_article(name)} #{name} of (" + list_join(flattened_types.map {|t| t.to_s(false) }) + ")" else "#{indefinite_article(name)} #{name} with type parameters (" + types.map {|t| t.to_s(true) }.join(", ") + ")" end end
Private Instance Methods
Source
# File lib/yard/tags/types_explainer.rb, line 174 def flattened_types types.flat_map {|t| t.is_a?(GroupType) ? t.types : [t] } end
A parsed type parameter that’s itself a union (from ‘Foo | Bar` in e.g. `Array<Foo | Bar, Baz>`) is a {GroupType}. Since this whole parameter list is already read as one flat union, absorb such a parameter’s own members into that same list instead of rendering it as a nested, parenthesized sub-union - the two mean the same thing here.
Source
# File lib/yard/tags/types_explainer.rb, line 164 def union? types.size <= 1 || UNION_COLLECTION_NAMES.include?(name) end
A single type parameter is never ambiguous (there’s nothing to distinguish a union from a positional type parameter when there’s only one), so it’s read as a union regardless of name; otherwise, only the known homogeneous-collection names are.