Volatile functions

8.09K viewsScripting
0

I’m writing a plugin function which is volatile – repeated calls to it return varying values – but I’m having trouble making this work.

As an example, consider the built-in rand() function. This returns a different value after each call (i.e. it has type “”IO double”” rather than “”double””, to borrow the Haskell convention).

Now if I write a Groovy function:

double groovy_rand() {
return new Random().nextDouble();
}

Then this behaves as I would expect, in that a 2D matrix with e.g. A1 = groovy_rand() produces a list of random numbers across category B.

But if I write a plugin function:

public class PluginRand extends AbstractQFunction {

public QValueList evaluate(QValueList[] args) {
return QValueFactory.createSingletonValueList(new Random().nextDouble());
}

public QType[] argumentTypes() {
return new QType[] {};
}

public QType returnType() {
return QType.cValueType;
}
}

then doing the same A1 = pluginrand() just produces a list across B of the same *first* random number.

How can I obtain the Groovy-like volatile behaviour from a plugin?

Many thanks.

0

Ah, that’s great, many thanks for the “under the hood” details Ben. So there is actually something special about how rand() works which is not re-implementable in a plugin. I think I can solve this with careful use of a dummy parameter, with rand() as a last resort.

This sounds like it’s one for the next version of QAPI: add a flag for whether the function should be cached or not.

Interesting however that groovy functions are not cached. This is what I need so an alternative to the hack is to do my plugin in a script. I actually prototyped it first in groovy, but ran into typing limitations.

To go off-topic now, the only reason I used a plugin function is that its parameters and return type can be a cValueType which can happily be either a String or a double.

On the other hand, I believe for a groovy function to be picked up by Quantrix, its parameters and return types have to be marked specifically as one of String or double (or int, Date etc.). Am I correct in saying we can’t yet define a polymorphic function in groovy that will work as Quantrix function?

Also I don’t think that varargs work for groovy functions, whereas a plugin can use cRepeatType to obtain a Quantrix function with varargs.

Thanks.

You are viewing 1 out of 8 answers, click here to view all answers.