Skip to content

Dummy variable _ #9343

Closed
Closed

Description

It'd be convenient to have a designated dummy variable _ (actually keyword), which would simply ignore everything stored into it. For example, you often want to deconstruct tuples like so

a, _, b, _, c = f()

where the _ values are uninteresting. Likewise, one may want to ignore function arguments, like so

function foo(a, _, b, _, c)
    return (a, b, c)
end

This is actually a syntax error right now, because two arguments have the same name, which makes sense if you actually want to read their values. The idea here is to instead discard them, and reading the value of _ would be a syntax error.

In theory, this is purely about code readability: when one sees _, one knows that the value is not used and thus won't need to be thought about any further; OTOH if one sees something else, then the value is probably going to be used further down the line and needs to be kept track of. One also won't need to behold the unsightly output of wetware gensym() implementations in unused function argument names. In practice though, there is also a performance difference with the current compiler:

function f1(x1, x2, x3, x4, x5)
    a, _, b, _, c = x1, x2, x3, x4, x5
    return (a, b, c)
end

function f2(x1, x2, x3, x4, x5)
    return (x1, x3, x5)
end

code_native(f1, (Int, String, Int, Int, Int))
code_native(f2, (Int, String, Int, Int, Int))

The assembly for f2 is significantly shorter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions