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

"Can't insert a first argument into" #36

Open
jtrakk opened this issue Oct 4, 2021 · 2 comments
Open

"Can't insert a first argument into" #36

jtrakk opened this issue Oct 4, 2021 · 2 comments

Comments

@jtrakk
Copy link

jtrakk commented Oct 4, 2021

julia> 5 |> @chain identity
5

julia> 5 |> @chain (x->x)
ERROR: LoadError: Can't insert a first argument into:
begin
    #= REPL[5]:1 =#
    x
end.

First argument insertion works with expressions like these, where [Module.SubModule.] is optional:

[Module.SubModule.]func
[Module.SubModule.]func(args...)
[Module.SubModule.]func(args...; kwargs...)
[Module.SubModule.]@macro
[Module.SubModule.]@macro(args...)
@. [Module.SubModule.]func

Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:33
  [2] insertionerror(expr::Expr)
    @ Chain ~/.julia/packages/Chain/wUcPF/src/Chain.jl:13
  [3] insert_first_arg(e::Expr, firstarg::Symbol)
    @ Chain ~/.julia/packages/Chain/wUcPF/src/Chain.jl:89
  [4] rewrite(expr::Expr, replacement::Symbol)
    @ Chain ~/.julia/packages/Chain/wUcPF/src/Chain.jl:104
  [5] rewrite_chain_block(block::Expr)
    @ Chain ~/.julia/packages/Chain/wUcPF/src/Chain.jl:211
  [6] var"@chain"(__source__::LineNumberNode, __module__::Module, block::Expr)
    @ Chain ~/.julia/packages/Chain/wUcPF/src/Chain.jl:245

The docs say

the result of the previous expression is used as the first argument in the current expression, as long as it is a function call, a macro call or a symbol representing a function

(x->x) is not a function call, macro call, or symbol representing a function, so an error is ok. But maybe it could support this anyway, expanding the API from "symbol representing a function" to "expression representing a function".

If you don't like it I can always go (x->x)(_), so it's not a very big problem, but it might be good.

@mahiki
Copy link

mahiki commented Apr 7, 2023

I ran into this today and found the error message to be confusing. I have a @chain of many transforms, and this is the problem:

if variable_1 != nothing transform(:column_1 => x -> variable_1, renamecols = false) end

So yeah now its obvious that @chain doesn't know where to put the dataframe. Fixed with:

if variable_1 != nothing transform(_, :column_1 => x -> variable_1, renamecols = false) end
                                  ^^^ -- underscore here

@jkrumbiegel
Copy link
Owner

The problem with writing out expressions to the user (in this case the one that Chain doesn't know how to insert the first arg into) is that they look different than what the user typed, so it can be a bit difficult to map back to the offending code. Maybe the error message can be improved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants