immutable-struct icon indicating copy to clipboard operation
immutable-struct copied to clipboard

Improve error message when passing a string and expecting an array

Open esanmiguelc opened this issue 5 years ago • 0 comments

Problem

# Example ImmutableStruct

Example = ImmutableStruct.new([:name])

Example.new(name: "somestring")
# => NoMethodError (undefined method `to_a' for "somestring":String)

Potential solution:

Check if we can call #to_a on the object that we are receiving. If we cannot, continue to fail but provide a more helpful message around what failed and how to fix it. Proposed language:

Expected Array for property: :name, Got: String

This one maintains the current API and behavior.

Alternative solution

Instead of using Object#to_a use Array(Object) which will wrap the object in an array or no-op if it's already an array.

esanmiguelc avatar Feb 11 '20 15:02 esanmiguelc