1
votes

I have an attr_reader that provides more than one variable, like so:

attr_reader :user, :series

I want to add a type signature to it, but Sorbet doesn't support multiple return types,

sig { returns(User, Series) }
attr_reader :user, :series

Is the only option to split them up like so?:

sig { returns(User) }
attr_reader :user
sig { returns(Series) }
attr_reader :series
1
I think you have no other choice than splitting. It'll also be a bit better to know which attribute return what kind of value because list of type & list of attribute aren't always perfectly aligned. - user11659763

1 Answers

1
votes

Yes, the only option is to split up your attribute declarations, just like how you would have done if you were defining separate getter/setter methods for them, unless all your attributes are of the same type.

The reason for this is that Sorbet, in the DSL phase of its operation, actually uses the sig on an attr_reader/attr_writer/attr_accessor declaration to define the sig on the synthetic methods that are produced by those declarations. Thus, a single getter for attr_reader, a single setter for attr_writer and a getter/setter pair for attr_accessor are generated synthetically and the sigs are applied to them.

As a result of this, this would be valid:

sig { returns(String) }
attr_reader :some_string_attr, :other_string_attr

but this would not be:

sig { returns(String, User) }
attr_reader :some_string_attr, :some_user_attr