How to use ternary operator for assigning a function

I want to do a function dispatch like below:

func real a(real s) {return s*s; }
func real c(real s) {return s-1; }
func myf = true ? a : c;

of course I want to use something other than true for the condition. But I get error for this. Any clean way to achieve this?

functions a and c expect a real argument…
you can also use a macros like

macro myf1(condition,s )( (condition ? s * s : s-1) ) //
macro myf2(s )( (true ? s * s : s-1) ) //

Thanks, one thing I forgot to mention is that I want to pass this myf as an argument to another function (e.g. like LinearCG which takes functions as input). Macros cannot handle this I guess.

Right. Then for a function

real K=1;
func myf = (true ? a(K) : c(K));

Note you have to be careful about the the type expected by the other function, and the type of the arguments as well… For example,
if the function should be real, with real argument

func real myf1(real aK) {return (true ? aa(aK) : cc(aK)); }

Thanks, I think the second form is good enough and works fine :slight_smile: