Void user defined functions that can pass objects by reference

discourse

#1

Is it, or will it be possible to have user defined functions that return a void where users can pass objects by reference? an example piece of code is below.

functions {
  void do_something(vector& vector_to_change, real parameter) {
    vector_to_change *= parameter;
  }
} 

I feel that could make some models more modular, but I may just be spit balling here.


#2

It is not currently possible because arguments are passed by const reference. I wouldn’t hold your breath for it to happen in the near future. Some of the Stan3 design has similar ideas about modularity, but I haven’t seen anything proposed like that for user-defined functions.


#3

Also, before Stan3, there will be tuples in the Stan language, so a user-defined function can just return a bunch of stuff in a tuple.


#4

Allgood, thanks for getting back so quick.


#5

Very unlikely to be added. The problem is that it makes the programming so much more complicated for average users when you start introducing these constant refernece vs. modifiable reference things.

We’re working on radically different ways to modularize for a revised Stan 3 language design, but that’s a long way off. The main goal there is to allow us to remove the resrictions blocks impose and to allow general subroutines that could introduce variables, etc., rather than the limited kind of function we have now.

An orthogonal update in the future is that we’ll be adding tuples. That’ll make it easier to write a function with heterogeneous outputs.