jkrumbiegel / Chain.jl

A Julia package for piping a value through a series of transformation expressions using a more convenient syntax than Julia's native piping functionality.
MIT License
368 stars 16 forks source link

Odd behavior with anonymous functions #20

Closed pdeffebach closed 3 years ago

pdeffebach commented 3 years ago

Here is an edge case I think is worth looking into

julia> using Chain

julia> x = 1
1

julia> @chain x begin 
       (t -> t + 1)
       end
ERROR: LoadError: Can't prepend first arg to expression t->begin
        #= REPL[76]:2 =#
        t + 1
    end that isn't a call.
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] insert_first_arg(::Expr, ::Symbol) at /home/peterwd/.julia/packages/Chain/HuSPl/src/Chain.jl:38
 [3] rewrite(::Expr, ::Symbol) at /home/peterwd/.julia/packages/Chain/HuSPl/src/Chain.jl:53
 [4] rewrite_chain_block(::Symbol, ::Expr) at /home/peterwd/.julia/packages/Chain/HuSPl/src/Chain.jl:80
 [5] @chain(::LineNumberNode, ::Module, ::Any, ::Expr) at /home/peterwd/.julia/packages/Chain/HuSPl/src/Chain.jl:113
in expression starting at REPL[76]:1

julia> @chain x begin 
       (t -> t + 1)(_)
       end
2
jkrumbiegel commented 3 years ago

Well the readme says there's only implicit function calling for symbols. Here you have a whole expression which, granted, is evaluated to a function. First-argument splicing only works for function calls and macros, not for arbitrary expressions. Also, why are you doing this if _ + 1 works?

And what should work I guess is making the function call explicit: (t -> t + 1)()

pdeffebach commented 3 years ago

That sounds good!

This is fine, i will close.

jkrumbiegel commented 3 years ago

Part of this is that the weirder the expressions that I allow in the macro, the harder it becomes to read, potentially. I think a single symbol is very easy to parse as a supposed function call, but your example would already be too close optically to an expression in which an underscore could hide somewhere, making it a different thing.