You can do:
let setIfChanged (oldVal: 'a byref) (newVal : 'a) = if newVal <> oldVal then oldVal <- newVal let mutable test = 42 setIfChanged &test 24
Based on your explanation of goals, I would recommend looking at Gjallarhorn . It provides signaling support over variable values ββand is designed to be extremely flexible in terms of what happens when the value changes. It can be used as the main mechanism by which you can easily create your own signals when something changes (if it does not yet provide what you need with the View module).
Edit:
Given your comment that the property in question is on the third side, one option would be to use dynamic support in F # to override the op_DynamicAssignment ( ?<- ) operator to dynamically send to a method, but raising your βnotificationβ in this process.
Given:
let (?<-) source property (value : 'a) = let p = source.GetType().GetProperty(property) let r = p.GetValue(source, null) :?> 'a if (r <> value) then printfn "Changing value" source.GetType().GetProperty(property).SetValue(source, value, null)
You can call someObj?TheProperty <- newVal , and you will see that the "Change value" value will only be printed if the value has really changed.
Internally, this works by using reflection to get the old value of the property and set the new one, so it will not have better performance characteristics, but since you use it to receive notifications about the change of the property type, this is probably not a problem.
source share