# Observable function

**URL:** https://discourse.julialang.org/t/observable-function/94646
**Category:** New to Julia
**Tags:** observables
**Created:** [February 14, 2023, 9:08pm UTC](https://discourse.julialang.org/t/observable-function/94646 "2023-02-14T21:08:34Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)
#### Post date: [February 14, 2023, 9:08pm UTC](https://discourse.julialang.org/t/observable-function/94646/1 "2023-02-14T21:08:34Z")

</div>

How do Observable functions work?

```julia
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?

---

<div class="post-metadata">

### Author: ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)
#### Post date: [February 14, 2023, 10:24pm UTC](https://discourse.julialang.org/t/observable-function/94646/2 "2023-02-14T22:24:35Z")

</div>

Your code fails with:

```julia
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](https://docs.julialang.org/en/v1/manual/types/#Types-of-functions)

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:

```julia
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](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-fields-with-abstract-type) 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…
