Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static array with non-obvious element type #6402

Open
straight-shoota opened this issue Jul 17, 2018 · 2 comments
Open

Static array with non-obvious element type #6402

straight-shoota opened this issue Jul 17, 2018 · 2 comments

Comments

@straight-shoota
Copy link
Member

straight-shoota commented Jul 17, 2018

When creating a static array with StaticArray.[] macro, the generic element type argument is inferred from the union of all macro argument types. However, there might be use-cases where the element type of the static array differs from the union of it's initial elements. This cannot be expressed using the [] macro.
As a workaround you need to use one of the constructors and assign the elements manually. It's more verbose but works. I'm not sure if this necessarily needs to change.

StaticArray#to_s and #pretty_print both stringify an instance as StaticArray[<element>] which suggests that this string representation can be used as a literal-like expression in source code representing the exact same value. But this is only the case is if T equals the union of all value types.
At least #pretty_print (and #inspect) should probably be more specific here because inspecting a static array should show the exact type. This could be improved by changing the print output to include T (for example StaticArray(Int32 | Float64)[1, 2, 3]) at least when it differs from the union of element types. However, this won't be usable in Crystal code because it is not a valid expression.

@jhass
Copy link
Member

jhass commented Sep 18, 2019

Can we make StaticArray(String, 2) {"a", "b"} work and switch inspect to that? https://carc.in/#/r/7ldf

@straight-shoota
Copy link
Member Author

Related to #5703

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants