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

Infer type from typed array #217

Open
HarrisonGrodin opened this issue Apr 2, 2017 · 1 comment
Open

Infer type from typed array #217

HarrisonGrodin opened this issue Apr 2, 2017 · 1 comment

Comments

@HarrisonGrodin
Copy link

The type of array elements should be inferred, both in the context of elements that were getindexed and inside of for loops.

For example, the code below should error, due to the fact that there is no field nonexistent in User and no method f(::User) in both cases.

julia> lintstr("""

       struct User
           email::String
       end

       f(x::Integer) = x

       users = User[]
       push!(users, User("[email protected]"))

       println(users[1].nonexistent)
       println(f(users[1]))

       for user = users
           println(user.nonexistent)
           println(f(user))
       end
       """)

Here, users is explicitly typed as User - this functionality should still work if users = [User("[email protected]")], though, as the array type is inferred from the element.

If an array is initialized with multiple types, the inferred type should be whatever Julia would promote the array to.

julia> lintstr("""

       numbers = [1, "two", 3.0]

       for number = numbers
           println(zero(number))
           println(identity(number))
       end

       """)

Here, numbers would be of eltype Any. zero(number) should error (because it is not defined for Any), but identity should pass.

@TotalVerb
Copy link
Collaborator

You're right that we should do more here. However we cannot in general error for

       for number = numbers
           println(zero(number))
       end

because it is a valid use case to have Any[1, 2, 3], which would also be inferred as a Vector{Any}. (Currently, we also use Any as a indicator for Lint not being able to figure out a type, and I think there would be too many spurious warnings if we started to warn about using things that are not defined on Any.)

The other suggestions are good though and should be possible in the medium term.

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

2 participants