Observable function

How do Observable functions work?

using GLMakie, Makie

rng = Observable(range(-10,10,1000))
f = Observable((x)->sin(x))
g = Observable((x)->x)
dep = @lift(Point2f.([($g(i), $f(i)) for i in $rng]))

rng[] = range(-10,5,1000)
f[] = (x) -> x^2

Changing rng works fine but not f.
What is the syntax for changing an observable function to a different function?

Your code fails with:

ERROR: MethodError: Cannot `convert` an object of type var"#9#10" to an object of type var"#1#2"

This is because each function has its own type, see the manual here: Types · The Julia Language

Therefore, you cannot change an Observable generated for one function to another function, because when you create an Observable it is, by default, done for a specific type. (you can find out by: ?Observable in the REPL).

So, you need to create Observables for your functions that are “wide enough” (in terms of types they accept).

The following works:

using GLMakie, Makie

rng = Observable(range(-10,10,1000))
f = Observable{Function}((x)->sin(x))
g = Observable{Function}((x)->x)
dep = @lift(Point2f.([($g(i), $f(i)) for i in $rng]))

rng[] = range(-10,5,1000)
f[] = (x) -> x^2

Note, however, that Function is not a concrete type, see here: Performance Tips · The Julia Language for information about the impact of it.

I guess that you are not running your plotting code in “must-have-highest-performance” setting, so it’s probably fine to have the abstractly typed Observables around…