# Would the Scala convention for anonymous function arguments be feasible?

**URL:** <https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659>\
**Category:** Internals & Design\
**Created:** [November 30, 2016, 8:32pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659 "2016-11-30T20:32:02Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [December 15, 2016, 6:42pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/21 "2016-12-15T18:42:03Z")

</div>

Is it the case that unused variables get optimized away? In that case, you could imagine

```julia
result = begin
    a = 1
    b = 2
    a + 1
    _ / b
end

```

getting lowered to

```julia
result = begin
    _ = a = 1
    _ = b = 2
    _ = a + 1
    _ = _ / b
end

```

but then optimized to

```julia
result = begin
    a = 1
    b = 2
    _ = a + 1
    _ / b
end

```

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [December 16, 2016, 6:51am UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/22 "2016-12-16T06:51:10Z")

</div>

> [@StefanKarpinski](#):
>
> I’m concerned about the impact that having a potential implicit data dependency between an expression and the next would have on optimization and code analysis.

I assumed that the presence of `_` would be dealt with at lowering time, such that

```julia
foo(x)
f(x)
g(_)
h(_)

```

is transformed to something like

```julia
foo(x)
$(Symbol("_#1")) = f(x)
$(Symbol("_#2")) = g($(Symbol("_#1"))
h($(Symbol("_#2"))

```

before inference even sees the code. It should be compatible with current optimizations, allow `_` to take different types (since there is a “bug” or missing capability for this in inference in v0.5), etc.

---

<div class="post-metadata">

**Author:** ![jballanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jballanc/32/328_2.png) [@jballanc](https://discourse.julialang.org/u/jballanc)\
**Post date:** [December 16, 2016, 12:35pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/23 "2016-12-16T12:35:08Z")

</div>

I agree with the “too much magic” concern, and I think it can be avoided if `_` becomes a sort of currying syntax. Actually, I think `_` as a curry “operator” could be useful many places, not just in the context of function chaining. Essentially, this means that `foo(_, b)` is transformed to `(x) -> foo(x, b)`.

The only question then is whether `foo(_, _)` should transform to `(x) -> ((y) -> foo(x, y))` or `(x, y) -> foo(x, y)`. The former is proper currying but perhaps the later would be more practically useful?

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [December 16, 2016, 12:50pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/24 "2016-12-16T12:50:04Z")

</div>

> The only question then is whether `foo(_, _)` should transform to `(x) -> ((y) -> foo(x, y))` or `(x, y) -> foo(x, y)`. The former is proper currying but perhaps the later would be more practically useful?

The third option which is compatible with the function chaining syntax proposed above is `foo(_, _)` transforms to `x -> foo(x,x)`.

This might not seem useful at first, but in function chaining it lets you use multiple returns (a tuple of results) more elegantly, such as `eig(matrix) |> foo(_[1], _[2])` more-or-less splats two output arguments from `eig` into slots of `foo`.

---

<div class="post-metadata">

**Author:** ![jballanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jballanc/32/328_2.png) [@jballanc](https://discourse.julialang.org/u/jballanc)\
**Post date:** [December 16, 2016, 1:45pm UTC](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659/25 "2016-12-16T13:45:32Z")

</div>

> [@andyferris](#):
>
> The third option which is compatible with the function chaining syntax proposed above is foo(\_, \_) transforms to x -\> foo(x,x).

Hmm…that _is_ closer to the Scala semantics, but seems to be a bit further from the notion of currying. It also requires that `_` behave, at least nominally, as a placeholder variable.

Ok, so if that’s the case (and if I may be so bold), I think the question boils down to: should `_` be an equivalent to `ans` or a placeholder that also triggers a currying transformation?

The former would, I suspect, require less change to the grammar of Julia but may require more refinement of `ans`’s scoping/semantics. The later involves less magic, but _would_ require a non-trivial transformation of the source/AST.

[Previous page](https://discourse.julialang.org/t/would-the-scala-convention-for-anonymous-function-arguments-be-feasible/659.md?page=1)
