You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 eltypeAny. zero(number) should error (because it is not defined for Any), but identity should pass.
The text was updated successfully, but these errors were encountered:
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.
The type of array elements should be inferred, both in the context of elements that were
getindex
ed and inside offor
loops.For example, the code below should error, due to the fact that there is no field
nonexistent
inUser
and no methodf(::User)
in both cases.Here,
users
is explicitly typed asUser
- this functionality should still work ifusers = [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.Here,
numbers
would be ofeltype
Any
.zero(number)
should error (because it is not defined forAny
), butidentity
should pass.The text was updated successfully, but these errors were encountered: