StaticLint.jl icon indicating copy to clipboard operation
StaticLint.jl copied to clipboard

Suggestion: Warn on `x -> foo(x)`

Open jakobnissen opened this issue 3 years ago • 3 comments

It's common to see people do e.g. filter(i -> isodd(i), arr). This is unidiomatic and should be filter(isodd, arr). It seems like it should be easy to add to the linter.

jakobnissen avatar Aug 15 '22 08:08 jakobnissen

I think this problem may not be as easy as it seems at first hand, because any type may be callable in Julia and the proposed suggestion only makes sense in general for Function subtypes.

For example:

julia> struct S end

julia> (::S)() = 3

julia> s = S()
S()

julia> f = (() -> s())
#1 (generic function with 1 method)

julia> s() == f()
true

julia> s isa Function
false

julia> f isa Function
true

Docs: https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects

nsajko avatar Oct 11 '22 16:10 nsajko

This is only relevant when the higher order function the functor is passed to has a strict type constraints on Function (instead of e.g. Base.Callable).

pfitzseb avatar Oct 11 '22 19:10 pfitzseb

This is only relevant when the higher order function the functor is passed to has a strict type constraints on Function

Yes, but this isn't "only" as far as I see. Furthermore, the constraint doesn't need to be on the direct callee - you'd have to check all potential indirect callees for such constraints.

instead of e.g. Base.Callable

Base.Callable is completely undocumented.

nsajko avatar Oct 11 '22 21:10 nsajko