The zero-argument anonymous function name

julia> bar(a,b,x...) = (a,b,x)
bar (generic function with 1 method)

julia> bar(1,2)
(1,2,())

Is () a zero-argument anonymous function that takes in varargs, making zero really mean any-argument anonymous function? If yes, then to me, the name zero-argument name just doesn’t fully capture/represent what it is. Anyone else feel this way?

If the zero-argument name changed to any-argument, I wouldn’t see the need to also use the name varargs. Or if zero changed to var-arguments, wouldn’t see the need to use zero and varargs.

No, it’s an empty tuple.

1 Like

So can () take var-args?

Below, can ()->3 also take varargs?

julia> o = ()->3 # ()->3 officially named as zero-argument
(::#1) (generic function with 1 method)

julia> o()
3

Is () an empty tuple that takes var-args?

The () in ()->3 is not a name. It is also akin to an empty tuple. Tuples represent the arguments of functions; an empty tuple means that the function takes no arguments.

f() = 3 and const f = ()->3 are basically defining the same thing.

1 Like

() is just parenthesis. In general it’s used when the syntax can otherwise be ambiguous.

In most cases it’s optional. You can write x->x instead of (x)->x, x, y instead of (x, y). Zero element tuples and zero argument functions needs the () since it’ll be hard to represent otherwise.

And

No, it’s wrong. You can try it yourself.

2 Likes

So (1,2,()) means bar won’t take any other args other than 1,2, that’s all it means?

julia> bar(a,b,x...) = (a,b,x)
bar (generic function with 1 method)

julia> bar(1,2)
(1,2,())

No it means you didn’t give it “any other args other than 1, 2”

Okay, so it states the obvious, could’ve been bar(1,2) => (1,2)?

Try bar(1, 2, 3) and bar(1, 2, 3, 4, 5) to understand what the splat (...) does.

No. for bar(1, 2) x = (). Please read the full section of https://docs.julialang.org/en/latest/manual/functions.html#Varargs-Functions-1

Yup, that exact Doc section led me here. Your previous comment about representing a zero element tuple drove the point home. It must be represented, even though there’s nothing you can do with it. Thanks.

julia> x = ()
()

julia> typeof(x)
Tuple{}

julia> length(x)
0
1 Like

Why does bar(1,2,3) => (1,2,(3,)), but

bar(1,2,3,4) => (1,2,(3,4)) instead of => (1,2,(3,4,))?

Because one element tuples need the additional comma to distinguish them from regular parentheses. Two element tuples are not ambiguous so the extra comma is not required. For example:

julia> 1 # just an integer
1

julia> (1) # also just an integer
1

julia> (1,) # one element tuple
(1,)

julia> (1, 2) # two element tuple
(1,2)

julia> (1, 2,) # also a two element tuple
(1,2)
2 Likes

On the flip side, it is often handy to “splice” the values contained in an iterable collection into a function call as individual arguments. To do this, one also uses … but in the function call instead:

x = (3,4);
bar(1,2,x...) => (1,2,(3,4))

How is it handy and why is splice quoted?

Because you might want to do this?

Nothing is quoted. You are just passing more than one argument for the vararg part. It’s the same as bar(1, 2, 3, 4) so x === (3, 4) and (a, b, x) === (1, 2, (3, 4)).

How is it handy: can you give an example?

Why is splice quoted: I pasted the blockquote directly from the Julia Doc. Does splice here differ from splice in the original sense?

Despite the definition of bar(a,b,x...) = (a,b,x), when x = (2,3,4), why does calling bar(1,x...) => (1,2,(3,4)) and not => (1,(2,3,4))? Is the function definition assigning b = 2, first iterable of the collection x? If yes, what’s the point of that?

You are already using it, i.e. bar(1, 2, x...). As for why you need it in a program that depends on what you are writing.

It’s unclear what you mean by splice and quote here.

Because calling bar(1, x...) is identical with bar(1, 2, 3, 4).

How have you used it?

Why was it written in the Doc as

On the flip side, it is often handy to “splice” the values contained

instead of On the flip side, it is often handy to splice the values contained?

I don’t follow. The explanation I came up with is that bar(1,x...) => (1,2,(3,4)) where x = (2,3,4) is so because bar(a,b,x...) = (a,b,x) was defined as such, where b must be assigned to something. Is that a correct interpretation?

Have you read What does the … operator do? in the FAQ?

1 Like