holoviz/param

Reactive expression executed too many times on initialization

maximlt opened this issue · 3 comments

While trying to improve a little the reactive guide I noticed that the pipeline below is executed too many times when initialized. Should it be executed at all?

from param import rx

namex = rx('bob')

def debug(value):
    print(value)
    return value

titlex = namex.rx.pipe(debug).title()

Output:

bob
bob
hoxbro commented

It makes some sense that it runs once to know the function's output is a string.

I think that has to do with .rx.pipe(func) because titlex = namex.title() doesn't seem to call .title() (had to subclass str as you can't patch str.title apparently!):
image

So it seems that it has to do with .rx.pipe(). I'd like to know if this is intended and if so what other operations cause the reactive expressions to be eagerly executed. What looks pretty obvious though is that it shouldn't be called twice 🙃

By default evaluation is only semi-lazy, i.e. in most cases where you're performing chained expressions it eagerly evaluates the previous step when applying a new operation. This is the only way to provide sensible and early feedback to the user that the expression they're building is valid. There could a completely lazy mode that does not evaluate (and therefore validate) any of the expression until the output is requested.

That said as you point out, there should only be a single evaluation in your example above.