New array syntax inside functions

I’m struggling to understand what the new (2.33.1) version of cmdstan expects from me.

I have code like this in the functions block

real logFunction(int k, real[] pars){
  real r = pars[1];
  real w = pars[2];
  real p = pars[3];
  real s = pars[4];
  real ans =  lbin(s, k, p) + size_lpmf(k | r, w);
  return(ans);
}

which gives

Error in '/tmp/Rtmpk6Sttv/model-60c058a9b77d.stan', line 4, column 21: Declaration
    of arrays by placing brackets after a type was removed in Stan 2.33.0.
    Instead use the array keyword before the type. This can be changed
    automatically using the auto-format flag to stanc

OK. So I then change to

real logFunction(int k, array[4] real pars){
  real r = pars[1];
  real w = pars[2];
  real p = pars[3];
  real s = pars[4];
  real ans =  lbin(s, k, p) + size_lpmf(k | r, w);
  return(ans);
}

which in turn gives

'/tmp/Rtmpk6Sttv/model-60c0517b60e9a.stan', line 2, column 2, parsing error:
   -------------------------------------------------
    12:    }
    13:  }
    14:  real logFunction(int k, array[4] real pars){
                                       ^
    15:    real r = pars[1];
    16:    real w = pars[2];
   -------------------------------------------------

"[" (list of commas) "]" expected in unsized return type of function definition.

What exactly is a “list of commas”? Nevermind, let’s list out the commas manually then, I suppose:

real logFunction(int k, array[,,,] real pars){
  real r = pars[1];
  real w = pars[2];
  real p = pars[3];
  real s = pars[4];
  real ans =  lbin(s, k, p) + size_lpmf(k | r, w);
  return(ans);
}

and now

'/tmp/Rtmpk6Sttv/model-60c052a33c004.stan', line 2, column 2:
   -------------------------------------------------
    13:  }
    14:  real logFunction(int k, array[,,,] real pars){
    15:    real r = pars[1];
           ^
    16:    real w = pars[2];
    17:    real p = pars[3];
   -------------------------------------------------

Ill-typed arguments supplied to assignment operator =: lhs has type real and rhs has type array[,,] real

How do I get the right specification here? Consulting the reference did very little for me, I’m afraid to say.

I’m confused as to (i) what is going on and (ii) where to look for guidance. I apologise if this is a silly question, but I’ve been coding in Stan for a long while and never had this much difficulty getting something this simple to work.

@rok_cesnovar @andrjohns

1 Like

real[] pars becomes array[] real pars

3 Likes

@andrjohns answered the question, but to provide more information the relevant documentation is here: 9.4 Argument types and qualifiers | Stan Reference Manual

1 Like

Thanks. What was throwing me off is that I have a bunch of other errors from other deprecated functions, and those were getting mixed up with this one. The deprecations in 2.33 caught me off guard.