Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Add type constraint: settable #671
Add type constraint: 'T : settable
We should allow:
let set (x : 'T when 'T : settable ) (value: 'T) = x <- value
This allows for cleanly binding viewmodel variables to UIs:
// If you have a mutable type that tracks changes (e.g. a Gjallarhorn mutable), // you can then do binding approximately like this: let bind signal (property: 'T when 'T : settable) = signal.Subscribe (fun s -> property <- s) // usage: do bind textSignal label.Text
There exists a standard approach to binding in WPF and Xamarin.Forms but it was implemented without type safety using magic strings, without making use of generics.
This is not critical for the application given. Without the type constraint you can still use the same approach, but it is a bit less clean:
// this is approximate let bind signal = signal.Subscribe do bind textSignal (fun s -> label.Text <- s)
The constraint may or not include F# mutable types. For the purposes of UI programming, it is OK to restrict it to settable properties on classes.
Estimated cost (XS, S, M, L, XL, XXL): M
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
Currently this fails to compile, as settable properties cannot be passed
"/home/runner/main.fs(8,3): error FS0257: Invalid mutation of a constant expression. Consider copying the expression to a mutable local, e.g. 'let mutable x = ...'."
I feel like fixing this would be a better way of fixing this problem.
It would cloud the semantics of