JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.58k stars 5.48k forks source link

union produces an array of type Any for generator with unknown eltype #48448

Open gaurav-arya opened 1 year ago

gaurav-arya commented 1 year ago

The code

union(i for i in 1:5)

produces

5-element Vector{Any}:
 1
 2
 3
 4
 5

The reason for the Any is likely because of eltype(i for i in 1:5) being Any, which probably makes sense. However, since union is eager, perhaps we should still expect the produced Vector to be concretely typed? For example, since union is called with only a single argument in the above case, the above is equivalent to unique(i for i in 1:5), or even just collect(i for i in 1:5) in the above case, both of which produce a concretely typed vector:

5-element Vector{Int64}:
 1
 2
 3
 4
 5

Version info:

Julia Version 1.9.0-beta2
Commit 7daffeecb8c (2022-12-29 07:45 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, tigerlake)
  Threads: 1 on 8 virtual cores
vmpyr commented 1 year ago

Hello! I am interested in taking up this issue. Since its my first time can someone help me with what I should start with? I have already forked and cloned the repo.

vmpyr commented 1 year ago

@gaurav-arya, I tried some tweaking by myself and I feel that the correct way to do this would be:

union([i for i in 1:5])

This gives the following output:

5-element Vector{Int64}:
 1
 2
 3
 4
 5

It will pass a Vector{Int64} into the union function. I don't know if the added functionality of not putting the square brackets in needed. Maybe the moderators can help with that. If it is really needed, I am ready to work on it but I might need some help

vmpyr commented 1 year ago

@udohjeremiah, does this need work?

udohjeremiah commented 1 year ago

@vmpyr, I assume you have been working on this and would love to follow up. If that's the case, thank you for your efforts so far, and I'm sure you've learned more about "Julia" on your way to your first working on this. As for the pull request (PR), I am not very familiar with it. I only added a label. So, I would advise you to provide your solution as a fix PR for this issue and then you can see what the community thinks of it.

vmpyr commented 1 year ago

I did try to understand the issue. For example, doing this with a string array also did the same thing:

julia> x = ["apple", "banana", "grapes"]
3-element Vector{String}:
 "apple"
 "banana"
 "grapes"

julia> union(i for i in x)
3-element Vector{Any}:
 "apple"
 "banana"
 "grapes"

julia> union([i for i in x])
3-element Vector{String}:
 "apple"
 "banana"
 "grapes"

I think either the user can use the union([i for i in x]) syntax or we have to change the code to give a concrete type, the latter seems the way forward imo. I can work on this issue but I will need for digging around before that. I will make a WIP Pull Request soon as you have suggested.