-
-
Notifications
You must be signed in to change notification settings - Fork 371
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
Feature request: variable-length argument lists to user-defined functions #2665
Comments
I already know that this implementation works, because I've been using it to simulate variable-length argument lists. |
One edit to the proposal: when |
Could user-defined Stan functions utilize the syntax and accessors for variadic C++ functions? |
Thanks for the proposal. The approach you propose is similar to what C++ does under the hood with variadic arguments (as @bgoodri alludes to). This has been generalized type safely to heterogeneous arguments with parameter packs in C++11, but I'm not sure we need that genrality in Stan. Given that we control the code generator, we could also implement the way we implement We're going slowly on language changes as we rebuild the parser, AST, and code generator in a language that'll be easier to work with than C++/Boost Spirit Qi going forward. Also, the method you provide is the way to implement this now, although it's clunky with the extra
When we get around to making Stan fully covariant, we'll allow |
There is a newer stanc3 issue which is similar: stan-dev/stanc3#1348 |
Summary:
Allow user-defined functions to have an argument list that ends with 0 or more arguments all of the same type. Call these optional arguments.
Description:
Currently there is no way to create user-defined functions that have a variable number of arguments. Here are some examples where that can be useful:
vector_append(v1, ..., vn)
: append n vectors of possibly differing lengths.block_diag(M1, ..., Mn)
: create a block diagonal matrix from n matrices of possibly differing shapes. This kind of thing shows up in state-space models.Additional Information:
Specifically, I am proposing the following:
T... v
for some typeT
, meaning 0 or more arguments of type T.size(v)
refers to the number of optional arguments, andv[i]
refers to thei
-th optional argument. It is illegal to usev
itself in any other way within the function body.So, for example, if we have a function definition
and a call
f(x, y1, y2)
, theny1
binds tov[1]
andy2
binds tov[2]
andsize(v)
is 2 in the body off
.There is a simple implementation of this proposal, which I'll describe by example. The above function definition for
f
is implemented asand the call
f(x, y1, y2)
is implemented asf(x, {y1, y2})
. This implementation does involve the creation of heterogeneous arrays (arrays whose elements may have differing shapes), but such arrays are never passed to any Stan function other thansize()
and the indexing operator.The text was updated successfully, but these errors were encountered: