f# type providers and INPC metaprogramming

f#, inotifypropertychanged, type-providers

Solution

I do not think you can achieve this with F# type providers (but I can see that that it would be nice). There is a number of problems and thoughts I can think of:

In your example, your `INPCTypeProvider<double>` would have to return something like a representation of a "property". This is not possible, because properties are not first-class values (unlike events). If they were, you wouldn't need a type provider...

Type provider cannot be parameterized by an existing type, so you cannot write say:

type MyNotifyObject = ProvideNotifiable<MyObject>

If this was possible, `ProvideNotifiable` could be a provider taking a type and building a new type with additional interface implementation. But this is not possible at the moment.

If you only need simple types, you might be able to create something like:

type MyObject = ProvideNotifiable<"MyProperty:int, OtherProperty:string">

This could generate a type with the two properties (defined in the string), but you cannot add anything more to the generated type (although, you might be able to generate actual type using generative provider and inherit from it...) I think this is probably the best you can do.

Aside from type providers, you can simplify the implementation of `INotifyPropertyChanged` a bit by using quotations. This F# snippet gives a simple example that makes the implementation safer.

Problem

I read the following article http://studentguru.gr/b/kron/archive/2012/09/26/c-template-metaprogramming-and-f-type-providers.aspx which shows a way to do compile time Fibonacci sequence generation with F# Type providers. The solution is in the article but the final program is ``` > cat .\fib.fs type fib = Playground.StaticFibonacci<100000> printfn "Fibonacci(100000) has %d digits" fib.Value.Length > fsc .\fib.fs -o fib.exe -r:.\FibonacciTypeProvider.dll –nologo > .\fib.exe Fibonacci(100000) has 20899 digits ``` This look very powerful. I was wondering if it would be possible to create a type provider for INPC ( INotifyPropertyChanged ) such that you instead of ``` open System.ComponentModel type MyObject() = let mutable propval = 0.0 let propertyChanged = Event<_, _>() interface INotifyPropertyChanged with [<clievent>] member x.PropertyChanged = propertyChanged.Publish member this.MyProperty with get() = propval and set(v) = propval <- v propertyChanged.Trigger(this, new PropertyChangedEventArgs("MyProperty")) ``` You might be able to write ``` open System.ComponentModel type MyObject() = let mutable propval = 0.0 let propertyChanged = Event<_, _>() interface INotifyPropertyChanged with [<clievent>] member x.PropertyChanged = propertyChanged.Publish member this.MyProperty : INPCTypeProvider<double> ``` So before I waste half a day digging into this perhaps somebody more informed could tell me I am wasting my time and this level of meta programming is just not possible.

Original source

Related problems