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

rand(sa, ::Int) returns Vector #308

Closed
jariji opened this issue Mar 23, 2024 · 5 comments
Closed

rand(sa, ::Int) returns Vector #308

jariji opened this issue Mar 23, 2024 · 5 comments

Comments

@jariji
Copy link

jariji commented Mar 23, 2024

Does this have to return Vector or could it return StructArray?

julia> sa = StructArray(a=[10,20,30], b=[100,200,300])
3-element StructArray(::Vector{Int64}, ::Vector{Int64}) with eltype @NamedTuple{a::Int64, b::Int64}:
 (a = 10, b = 100)
 (a = 20, b = 200)
 (a = 30, b = 300)

julia> rand(sa, 2)
2-element Vector{@NamedTuple{a::Int64, b::Int64}}:
 (a = 30, b = 300)
 (a = 20, b = 200)
@aplavin
Copy link
Member

aplavin commented Mar 27, 2024

Agree that it would make sense – but unfortunately there doesn't appear to be a consensus :(
#269
JuliaLang/julia#49156

@jariji
Copy link
Author

jariji commented Mar 27, 2024

After reading the Julia thread I think they're right. If rand(x, ::Int) is going to work on range then rand shouldn't preserve type. It's better to have strict rules than best-effort.

For a type-preserving rand, another function should be introduced, which will be undefined on UnitRange.

@aplavin
Copy link
Member

aplavin commented Mar 27, 2024

Lots of function do type preservation only when it makes sense, returning the closest reasonable type – don't see how rand is special in that regard.

@jariji
Copy link
Author

jariji commented Mar 27, 2024

map and filter come to mind, about which my opinion is the same. What else?

@aplavin
Copy link
Member

aplavin commented Mar 27, 2024

There are so many it's hard to decide where to start :)
Two very different examples:
sqrt preserves floats but returns float when passed an int
vcat preserves many array kinds (incl structarrays), but returns a vector when passed a range

@jariji jariji closed this as completed Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants