Support Varargs
Having varargs available in the 1.0 release of Elixir would rock! Think of its frequent usage in Ruby (e.g. options hashes).
Joxa has it. Hence, there's no execuse that it is not possible to implement it on the BEAM :P
@KlausTrainer Thanks for the suggestion. I still need to checkout how Joxa implements it, but right now I see only two ways to implement this and none of them pleases me:
-
At runtime we would check if we are calling a function with more arguments than it receives. If so, we compact the arguments at runtime to fit the rest. I dislike this because it adds a runtime cost on function invocation. I would be happy to live with such costs if we had things like JIT so the cost would happen just on first dispatch, but we don't;
-
At compilation time, defining a rest function would define many others functions so we can receive an arbitrary number of arguments. For example, doing
go(*rest), would definego/0,go/1,go/2and so on. I dislike this because it would end up polluting compiled modules;
That said, I will leave this open in case someone has a better idea of how to implement this. We could potentially allow this feature just on macros, because the runtime cost is not that important, but having a feature half baked doesn't seem like a good idea as well.
Ah, if i remember correctly, @tarcieri also implemented this in reia.
I belive that 1) is not necessary, as that can definitely be handled at compile time. Also, explicitely defining the same function again and again like you said in 2) shouldn't be necessary.
What Joxa does is to add annotations to functions at compile time. If there's an annotation indicating that it's a vararg function, the arguments are simply collapsed down. This can happen at compile time.
See here for an introduction on how Joxa handles varargs.
Thanks for the link. However, it just mentions the semantics and not the implementation.
If there's an annotation indicating that it's a vararg function, the arguments are simply collapsed down. This can happen at compile time.
But not always right? This assumes the module I am dispatching to is available and defined at compile time, which is not necessarily true in Erlang. If we can't detect the vararg at compile time, we should likely fallback to a runtime check and that would add an unnecessary cost.
Yeah, the last section "Variables that Refer to Functions" in the page I've linked to mentions that there are cases where the resolution has to be done at runtime. However, I'm wondering if those cases wouldn't be relatively rare anyway.
This assumes the module I am dispatching to is available and defined at compile time, which is not necessarily true in Erlang.
It's true that Joxa takes the assumption that Joxa modules are available at compile time. I'm quite aware of the fact that (apart from the usage of plain old Erlang modules) this breaks Erlang's compilation semantics here.
I think here we end up at the actual core issue regarding this feature, i.e., do we want to break existing semantics or not? Furthermore, is there another acceptable solution for varargs that can be implemented without having to break compilation semantics?
I think here we end up at the actual core issue regarding this feature, i.e., do we want to break existing semantics or not? Furthermore, is there another acceptable solution for varargs that can be implemented without having to break compilation semantics?
Exactly! This is why I believe we shouldn't decide this right now. I want to understand better how people will build Elixir applications and explore if we have better options. Thanks for the discussion and feedback!
PS: Are you one of the developers behind Joxa? :)
Ok, I understand. Thanks for the discussion, too!
No, I'm actually not affiliated with Joxa. I just had a look into it a few weeks ago when I wanted to understand how they've implemented varargs ;)