Volatile functions

7.87K 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

Apollo,

Just a quick and maybe a silly idea: Create a seperate function inlcuding the code

[code:o88cyp90] return new Random().nextDouble() [/code:o88cyp90]

Your class looks then like this:

[code:o88cyp90]
public class PluginRand extends AbstractQFunction {

public QValueList evaluate(QValueList[] args) {
return QValueFactory.createSingletonValueList(getRandomNumber());
}

private static double getRandomNumber()
{
return new Random().nextDouble();
}

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

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

[/code:o88cyp90]

But I suppose this won’t work as I hope because the function createSingletonValueList() just creates one single value.

All the best,
Dominik

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

Latest Questions